About this lesson
Selenium Python double_click with ActionChains: trigger a double-click handler
Selenium Python AssignmentsAdvancedMouse and Keyboard
How it works
A double click is two quick clicks plus a separate dblclick event. Two separate click() calls do not fire that event. The ActionChains method double_click sends the right sequence. You pass the target element, then call .perform() to run the stored steps.
You can see the difference on this page. A normal click() on the button left its text unchanged. Only the double click switched it. When a feature listens for the double click event, a single click is the wrong tool.
Every chain method returns the chain, so more steps can follow before perform(). The final call returns None, so do not expect a result from it. The gesture belongs to the chain only. Asking the element itself raises AttributeError: 'WebElement' object has no attribute 'double_click'.
You can keep the variable you looked up before the gesture. The button stays on the page, its text changes and it becomes disabled, and reading .text again still works. A page that reloads is different, and old elements go stale there.
About the assignment
Show that a double click changes a button. Read its label, perform the gesture, and read the label again.
What you will do
- Locate the double-click button by its id and print its label under the first heading from the task.
- Build a chain that double clicks that element and run it.
- Read the same element's
.textagain and print it under the second heading.
Common mistakes
Using a single click
On this page
click()leaves the text as it was. The handler waits for the double click.Building the chain and stopping
Without
perform()the object is only a plan. The text afterwards is unchanged, and Python raises nothing.Calling the gesture on the element
The line fails with
AttributeError, since the method belongs toActionChains.
In a real test suite
Real apps use double click for rename-in-place and spreadsheet-like cells. Suites hide the chain in a helper that takes an element, so every test calls one clear function.
The next lesson swaps the gesture for a right click on a neighbouring button.
Related lessons
- Click a Button and Read the New TextA plain
click()and a text read, the baseline this lesson goes beyond. - Hover and Read the Color ChangeIntroduces
ActionChainsandperform()with a hover. - Right-Click a ButtonRight click on the same page with the same read-before-and-after shape.
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/Elements/Buttons")
# Double-click #doubleClick and print both texts
finally:
driver.quit()