Skip to content
CodeKruPro

Click and Hold a Box

60/80

Click and Hold a Box

click_and_hold keeps the mouse button down. pause waits inside the action. Holding #box2 changes its text.

Task:

  1. Click and hold #box2 for more than one second
  2. Print the box text
  3. Release the mouse

Print the lines below exactly.

Expected output:

During hold: Holding Box 2

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

  1. Open the drag and drop page and find the second box by its id.
  2. Build one chain that presses and holds on the box, waits longer than the page's one second threshold, and runs.
  3. Print the box text with the label from the task while the button is still down.
  4. 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.

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.

main.py
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()