0
0
Selenium Pythontesting~5 mins

Right click (context_click) in Selenium Python

Choose your learning style9 modes available
Introduction

Right click lets you open a menu or perform special actions on a webpage element, just like using the mouse's right button.

When you want to test if a context menu appears on right-clicking an element.
When you need to simulate user actions that require right-click, like opening links in new tabs.
When verifying that right-click options work correctly on buttons or images.
When automating tasks that depend on right-click menus in web applications.
Syntax
Selenium Python
from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.context_click(element).perform()

context_click(element) means right-click on the given element.

You must call perform() to execute the action.

Examples
Right click on the specified element.
Selenium Python
actions.context_click(element).perform()
Right click at the current mouse position (no element specified).
Selenium Python
actions.context_click().perform()
Sample Program

This script opens a demo page with a button. It right-clicks the button to open a context menu. Then it checks if the menu is visible and prints the result.

Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time

# Setup driver (Chrome example)
driver = webdriver.Chrome()
driver.get('https://swisnl.github.io/jQuery-contextMenu/demo.html')

# Find the button to right click
button = driver.find_element(By.CSS_SELECTOR, 'span.context-menu-one.btn.btn-neutral')

# Create ActionChains object
actions = ActionChains(driver)

# Right click on the button
actions.context_click(button).perform()

# Wait to see the context menu
time.sleep(2)

# Check if context menu is visible
menu = driver.find_element(By.CSS_SELECTOR, 'ul.context-menu-list')
print('Context menu visible:', menu.is_displayed())

# Close the browser
driver.quit()
OutputSuccess
Important Notes

Always import ActionChains from selenium.webdriver to use context_click.

Use explicit waits if the context menu takes time to appear instead of time.sleep() for better reliability.

Summary

Right click is done using ActionChains.context_click(element).perform().

This simulates the mouse right button on a webpage element.

It helps test context menus and special right-click actions.