About this lesson
Selenium Python click_and_hold with pause: keep the mouse down and read the box text
Selenium Python AssignmentsAdvancedMouse and Keyboard
How it works
click_and_hold presses the left mouse button on an element and keeps it down until a chain runs release(). Between those moments you can inspect the page, which is how you test features that react to a long press.
The page here changes the box text after the button has been down for about a second. Selenium does not wait for that, because a chain finishes as soon as its last action is sent. The pause method adds a delay inside the chain, in seconds. With a pause of 0.4 the text was still the original. With a pause clearly over one second it had changed. A time.sleep after perform() also worked, since the mouse stays down between chains.
pause takes a number and returns the chain. A string fails: "1" raises InvalidArgumentException, and "abc" raises ValueError. perform() returns None, so the text comes from the element afterwards, through .text.
After the release the text went back to normal. That shows the text describes a state of the page, not a permanent change.
About the assignment
Hold the mouse down on the second box long enough for the page to react, read its text while still holding, then let go.
What you will do
- Open the drag and drop page and find the second box by its id.
- Build one chain that presses and holds on the box, waits longer than the page's one second threshold, and runs.
- Print the box text with the label from the task while the button is still down.
- Start a new chain that releases the mouse and run it.
Common mistakes
No pause in the chain
Right after
perform()the text still shows its starting value, because the page needs time. You print the old text.Pausing for too short a time
A pause of 0.4 seconds is below what the page needs. Use a delay clearly above one second.
Passing the wrong type to pause
A string such as "1" raises
InvalidArgumentException: invalid argument: missing command parameters. Use a number of seconds.
In a real test suite
Real suites avoid fixed sleeps where they can. For long press features, teams keep the delay as a named constant and confirm the page state with a wait. This lesson uses a fixed pause because the page itself is time based.
The next two lessons turn to the keyboard, with select all and the shift key.
Related lessons
- Drag a Box onto a Drop ZoneDrag and drop uses the same page and the same press and release steps.
- Double-Click a ButtonAnother gesture sent through
ActionChains. - Select All and Replace Text with the KeyboardStarts the keyboard lessons, sending
Keysthroughsend_keys.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
try:
driver.get("https://testkru.com/Interactions/DragAndDrop")
# Hold #box2 and print its text during the hold
finally:
driver.quit()