Skip to content
CodeKruPro

Right-Click a Button

58/80

Right-Click a Button

context_click is a right-click. This page changes the button when mouse button 2 goes down.

Task:

  1. Print the text of #rightClick
  2. Right-click it
  3. Print the text again

Print the lines below exactly.

Expected output:

Before: Right click on me
After: I was right-clicked!
Read full tutorial on Codekru

About this lesson

Selenium Python context_click: right-click a button and read what changed

Selenium Python AssignmentsAdvancedMouse and Keyboard

How it works

A right click is called a context click, because it normally opens a context menu. In Selenium Python the method is ActionChains.context_click. It takes the element to click, and like every action it only runs when the chain ends with .perform().

Notice what is missing. A WebElement has no context_click attribute, so asking for it there raises AttributeError. The gesture belongs to the chain object, never to the element.

This page reacts to the mouse button number, not to a menu. Button 2 going down triggers the text change. If an app draws its own menu in HTML, that menu is made of ordinary elements, and you click its items after the right click.

Steps can be mixed in one chain. A right click followed by a double click on another button ran both, in order. For clarity, keep each gesture in its own chain unless the order itself is what you test.

About the assignment

Print the label of a button, send it a right click, and print the label again so the change shows in your output.

What you will do

  1. Find the right-click button by its id and read its .text. Print it after the first label.
  2. Create a chain that context clicks that element, then run it.
  3. Read the text of the same element again and print it after the second label.

Common mistakes

  • Using a left click

    The ordinary click() left the text unchanged here. The handler only reacts to the secondary button.

  • Calling the gesture on the element

    button.context_click raises AttributeError: 'WebElement' object has no attribute 'context_click'. Start from ActionChains(driver).

  • Skipping perform()

    The chain exists but no mouse event was sent. The text stays as it was, and no error tells you why.

In a real test suite

In suites, a right click usually opens an app-drawn menu, such as rename or delete on a file list. A page object method performs the gesture and then waits for the menu with WebDriverWait before choosing an item.

Lesson 59 moves from clicks to dragging, where the browser window size starts to matter.

To try one of these calls on any page without a task, open the Online Selenium Python Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

main.py
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
try:
    driver.get("https://testkru.com/Elements/Buttons")
    # Right-click #rightClick and print both texts
finally:
    driver.quit()
Read the full tutorial