How to do double and right-click in Selenium

Selenium is a browser automation module provided by Python. It is used to automate the testing of web applications. In this Answer, we'll learn how to perform the double-click and right-click actions.

We can perform these actions on DOM elements using the methods double_click() and context_click() provided by the ActionChains class.

Syntax

Here's the syntax for these methods:

#syntax for double click
actions_chains_object.double_click(provide your element here).perform()
#syntax for right click
actions_chains_object.context_click(provide your element here).perform()

Now, let's take a look at an example of this.

Code example

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
 
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
prefs = {"download.default_directory": "."}
options.add_experimental_option("prefs", prefs)

#get instance of web driver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

time.sleep(1)

#provide website url here
driver.get("https://demoqa.com/buttons")

#sleep for 2 seconds
time.sleep(2)

#find element using id
double_click_ele = driver.find_element("id","doubleClickBtn")

#ActionChains object
actions = ActionChains(driver)

#perform double click
actions.double_click(double_click_ele).perform()

time.sleep(2)

right_click_ele = driver.find_element("id","rightClickBtn")

#perform  right click
actions.context_click(right_click_ele).perform()

time.sleep(10)

Code explanation

  • Line 15: We create an instance of a web driver and assign it to the variable driver.

  • Line 20: We open a webpage using the get() method.

  • Line 26: We find an element using its id doubleClickBtn, and assign it to the variable double_click_ele.

  • Line 29: We create an instance of the ActionChains class, and assign it to the variable actions.

  • Line 32: We perform a double-click action on the element double_click_ele using the double_click() method.

  • Line 36: We find an element using its id rightClickBtn, and assign it to the variable right_click_ele.

  • Line 39: We perform a right-click action on the element right_click_ele using the context_click() method.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved