How to Double Click in Selenium Python: Syntax and Example
To double click in Selenium Python, use the
ActionChains class with its double_click() method on a web element, then call perform() to execute. This simulates a user double-clicking on the specified element.Syntax
The ActionChains class in Selenium Python allows you to chain multiple actions. To double click, you first create an ActionChains object with the driver, then call double_click(element) where element is the target web element. Finally, call perform() to execute the action.
ActionChains(driver): Creates an action chain object.double_click(element): Adds a double click action on the element.perform(): Executes all actions in the chain.
python
from selenium.webdriver import ActionChains # Create ActionChains object actions = ActionChains(driver) # Double click on the element actions.double_click(element).perform()
Example
This example opens a webpage, finds a button by its ID, and performs a double click on it using ActionChains. It demonstrates the full setup and execution of the double click action.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains import time # Setup WebDriver (make sure chromedriver is in PATH) driver = webdriver.Chrome() driver.get('https://api.jquery.com/dblclick/') # Switch to the demo frame containing the box to double click driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, 'iframe.demo-frame')) # Find the box element box = driver.find_element(By.CSS_SELECTOR, 'div#target') # Create ActionChains object actions = ActionChains(driver) # Perform double click on the box actions.double_click(box).perform() # Wait to see the effect time.sleep(3) driver.quit()
Output
The webpage opens, the blue box inside the frame changes color after the double click, then the browser closes after 3 seconds.
Common Pitfalls
Common mistakes when double clicking in Selenium Python include:
- Not calling
perform()afterdouble_click(), so the action never executes. - Trying to double click on an element that is not visible or interactable, causing exceptions.
- Not switching to the correct frame or window if the element is inside an iframe or popup.
- Using stale element references if the page reloads or changes before the action.
Always ensure the element is visible and ready before performing the double click.
python
from selenium.webdriver import ActionChains # Wrong: missing perform() actions = ActionChains(driver) actions.double_click(element) # This does nothing # Right: actions.double_click(element).perform()
Quick Reference
| Step | Code Snippet | Description |
|---|---|---|
| 1 | actions = ActionChains(driver) | Create ActionChains object |
| 2 | actions.double_click(element) | Add double click action on element |
| 3 | actions.perform() | Execute the action chain |
Key Takeaways
Use ActionChains with double_click(element).perform() to double click in Selenium Python.
Always call perform() to execute the action chain.
Ensure the target element is visible and interactable before double clicking.
Switch to the correct iframe or window if the element is inside one.
Avoid stale element references by locating elements fresh before actions.