Sometimes, when working with web applications, we may encounter various scenarios where we need to double-click on a particular element to perform some actions. A common example of this is when we use document editors, where we may need to double-click on text elements to activate editing mode or to view formatting options. This post will explore a couple of ways to perform double-clicks on elements using Selenium in Python.
- Using ActionChains
- and by using execute_script() function
Let’s see both of them one by one
Using ActionChains
ActionChains class in Selenium Python provides a method named double_click(), which is specifically used for double-clicking on an element. This method is widely used for double-clicking on an element in Selenium. Let’s see how it works with an example.
We will double-click on the highlighted element in the below image. This element is present on our Selenium playground website page: https://testkru.com/Elements/Buttons.
- First, we can use the element’s id (“doubeClick”) to locate it using the find_element() method provided by Selenium.
element = driver.find_element(By.ID, 'doubleClick')
- We can then use ActionChains doube_click() method to double-click on the element
actions = ActionChains(driver)
actions.double_click(element).perform() # double-click on the element
Whole code
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True) # Using this, so the browser doesn't auto-close itself
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/Buttons")
element = driver.find_element(By.ID, 'doubleClick')
actions = ActionChains(driver)
actions.double_click(element).perform() # double-click on the element
Above code will double-click on the specified element, which is also indicated by the change of the button’s text to “I was double-clicked!”
Using execute_script() method
execute_script() in Selenium Python helps execute the javascript code. So, if we can find a way to double click on an element using javascript, then we can execute that script with the help of the execute_script() method and perform our desired action.
And Below javascript code can help us in double-clicking on an element.
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('dblclick', true, true);
arguments[0].dispatchEvent (clickEvent);
where arguments[0] is the web element.
and now, we can simply execute the javascript code using the execute_script() method.
javascript = """var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('dblclick', true, true);
arguments[0].dispatchEvent (clickEvent);"""
driver.execute_script(javascript,element)
Whole code
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True) # Using this, so the browser doesn't auto-close itself
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/Buttons")
element = driver.find_element(By.ID, 'doubleClick')
javascript = """var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('dblclick', true, true);
arguments[0].dispatchEvent (clickEvent);"""
driver.execute_script(javascript,element)
print("Text written on the element:", element.text)
Output –
Text written on the element: I was double-clicked!
We can see from the output that the element was double-clicked
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.
Related Articles –