Introduction
Many web applications provide additional options through a right-click (context menu) instead of a normal left mouse click. Right-click menus are commonly used in file managers, dashboards, editors, and administrative applications.
Since Selenium cannot perform a right-click using normal WebDriver commands, it uses the ActionChains class. The context_click() method simulates a real user performing a right-click on an element.
In this tutorial, you’ll learn how to automate right-click operations using Selenium with Python, along with practical examples, real-world scenarios, common mistakes, and best practices.
What is Right Click?
A Right Click (also called a Context Click) is the action of pressing the right mouse button on an element.
Example:
User Right Clicks
│
▼
Target Element
│
▼
Context Menu / Alert Appears
Many applications display additional options only after a right-click.
Why Automate Right Click?
Right-click automation helps you:
Test context menus.
Verify custom right-click actions.
Test administrative tools.
Validate desktop-style web applications.
Automate advanced user interactions.
How Selenium Performs Right Click
Selenium uses the ActionChains class.
The most commonly used method is:
ActionChains(driver)\
.context_click(element)\
.perform()
This simulates a right-click on the specified element.
Example
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
# Topic: 33. Actions Class - Right Click
# Practice site: https://the-internet.herokuapp.com/context_menu
# Run: pytest -s 33_examples/test_03_right_click.py
#
# context_click performs a right-click. This page opens an alert when the box is
# right-clicked.
def test_right_click_opens_context_alert:
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/context_menu")
box = driver.find_element(By.ID, "hot-spot")
ActionChains(driver).context_click(box).perform()
alert = driver.switch_to.alert
assert "context menu" in alert.text.lower()
alert.accept()
finally:
driver.quit()
Understanding the Code
Import Required Libraries
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
These modules are required to launch the browser, locate web elements, and perform advanced mouse actions.
Create a Chrome Browser Instance
driver = webdriver.Chrome()
Starts a new Chrome browser session.
Open the Practice Website
driver.get("https://the-internet.herokuapp.com/context_menu")
Navigates to the Context Menu practice page.
Locate the Target Element
box = driver.find_element(
By.ID,
"hot-spot"
)
Locates the rectangular area that responds to a right-click.
Perform the Right Click
ActionChains(driver)\
.context_click(box)\
.perform()
Performs a right-click on the target element.
This action causes a JavaScript alert to appear.
Switch to the Alert
alert = driver.switch_to.alert
Switches Selenium’s control from the webpage to the alert dialog that appears after the right-click.
Verify the Alert Text
assert "context menu" in alert.text.lower()
Checks that the alert message contains the text “context menu”.
Using .lower() makes the comparison case-insensitive.
Accept the Alert
alert.accept()
Closes the alert by clicking the OK button.
Close the Browser
driver.quit()
Closes the browser and ends the WebDriver session.
Practical Example
Suppose an enterprise file management application displays file options after a right-click.
The automation script:
Opens the file manager.
Right-clicks a file.
Verifies that the context menu appears.
Automation Testing Example
Consider an online image editor.
Right-clicking an image displays editing options.
The automation script:
Opens the editor.
Right-clicks an image.
Verifies that the editing menu or dialog appears.
Real-World Example
Right-click functionality is commonly used in:
File management systems
Content Management Systems (CMS)
Enterprise dashboards
Image editors
Online IDEs
Administrative portals
Desktop-style web applications
Examples include context menus for files, folders, tables, images, dashboards, and administrative controls.
Advantages of Right Click Automation
Tests context menus.
Simulates real user interactions.
Verifies hidden options.
Supports enterprise web applications.
Improves automation coverage.
Common Mistakes Beginners Make
Trying to Use click() Instead of Right Click
A normal click() performs a left-click.
Use context_click() for right-click actions.
Forgetting to Call perform()
Without perform(), the ActionChain will not execute.
Forgetting to Handle the Alert
If a JavaScript alert appears, Selenium must switch to the alert before interacting with it.
Ignoring Synchronization
If the context menu or alert appears with a delay, use an Explicit Wait before interacting with it.
Best Practices
Use
ActionChainsfor right-click operations.Call
perform()to execute the action.Handle alerts or context menus immediately after the click.
Verify the expected result before continuing.
Keep right-click tests simple and readable.
Conclusion
Right-click is an important advanced user interaction that reveals context menus and additional functionality in many web applications. Selenium’s ActionChains class makes it easy to automate right-click operations using the context_click() method. Mastering right-click automation is essential for testing enterprise applications, administrative tools, and interactive web interfaces.
Frequently Asked Questions (FAQs)
What is a Right Click?
A right click (or context click) is the action of pressing the right mouse button on an element to display additional options.
Which Selenium class performs a right click?
The ActionChains class is used to perform right-click operations.
Which method performs the right click?
Use:
context_click(element)
to perform a right-click on an element.
Why is perform() required?
perform() executes the actions that have been added to the ActionChain.
Without it, the right-click action will not occur.
Where is right-click commonly used?
Right-click is commonly used in file managers, dashboards, image editors, administrative portals, online IDEs, CMS applications, and enterprise software.
Key Takeaways
Right-click is also called a context click.
Selenium performs right-click actions using
ActionChains.Use
context_click()to simulate a right mouse click.Always call
perform()to execute the action.Handle any resulting alert or context menu after the click.
Right-click automation is an essential Selenium skill for testing interactive web applications.
