How to Right-Click and Do Other Click Actions in Selenium

The world is indeed becoming more agile, and automated tests are a must in order to keep up. By automating…

Testim
By Testim,

The world is indeed becoming more agile, and automated tests are a must in order to keep up. By automating your tests, you can increase your speed and efficiency while ensuring that your product is of the highest quality. Automated tests also allow you to easily and quickly regression test your product, which is essential for maintaining a high level of quality.

This post will teach you how to right-click and do other click actions in Selenium. Selenium is one of the most popular frameworks for writing automation tests. However, it doesn’t always have the features you need, so you have to find ways to do things on your own.

What Is Click Action in Selenium?

Click action in Selenium is used to click on an element on a webpage. It’s one of the most commonly used actions while testing web applications. Click action can be performed using the click() method of the WebElement class. This method accepts a WebDriver instance as an argument and clicks on the element.

icon = driver.find_element_by_xpath("//div[@class='icon']")
icon.click()

Purpose of Click Action in Selenium

Click is one of the most commonly used actions in Selenium. It simulates a user clicking on an element on a webpage.

Click is often used in conjunction with other actions, such as type or select. For example, you may use click to open a drop-down menu, then use type to enter some text into a search box. Or, you may use click to select an item from a list, then use select to choose an option from a drop-down menu.

Prerequisites

Before we get started, make sure you mark the prerequisites checkboxes so you can follow along without any issues. This will help ensure that you have the necessary tools and knowledge to complete the task at hand. 

  1. Python installed locally on your machine
  2. Understanding of Python and virtual environments (minimal)
  3. Basic understanding of Selenium

I’ll use macOS for this guide. Make sure you understand the commands and use them based on your operating system.

Setting Up a Python Project

First, let’s create a directory for this project. We’ll call it “selenium-right-click.”

mkdir selenium-right-click

Next, we’ll create a virtual environment using pipenv. This will allow us to install the dependencies we need for our project in an isolated space. To do this, let’s first install pipenv. We can do this using the pip (Python package manager):

pip install pipenv

Once pipenv is installed, let’s use the pipenv shell for all the subsequent commands.

pipenv shell

pip install Selenium

How Do You Right-Click in Selenium?

To right-click in Selenium, you can use the Actions class. This class has a method called contextClick, which takes an element as a parameter. This method will simulate a right-click on the given element.

Let’s create a main.py file and start writing code.

from selenium import webdriver
from Selenium.webdriver.common.keys import Keys
from Selenium.webdriver import ActionChains

# Main Function

if __name__ == '__main__':

   # Provide the path of chromedriver present on your system.
   driver = webdriver.Chrome(executable_path="chromedriver")
   action = ActionChains(driver)
   
   # Provide the URL of the website you want to visit.

   link = 'https://www.google.com/'
   driver.get(link)
   search = driver.find_element('name', 'q')
   search.send_keys('testim io')
   search.send_keys(Keys.RETURN)

   # XPath of Images Hyperlink
   images_link_xpath = '//a[text()="Images"]'
   image_tab = driver.find_element('xpath', images_link_xpath)
   image_tab.click()

   # XPath of the testim.io image
   image_xpath = '//a[@jsname="sTFXNd"]'
   required_image = driver.find_element('xpath', image_xpath)
   action.context_click(required_image).perform()

Understanding the Code Snippet

  1. First, we’ve imported all required packages.
  2. Then, we create a driver instance using ChromeDriver. In order to create a driver using ChromeDriver, we first need to ensure that we have the ChromeDriver executable in our system PATH.
  3. Once that’s done, we create an instance of ActionChains. The ActionChains class provides a way to simulate user interaction with the browser. This is useful for cases where there’s no easy way to automate a task using the Selenium API.
  4. We’ll now use the driver.get method to visit the URL. This’ll take us to the desired page, where we can then interact with the page elements.
  5. Then, we use find_element, send_keys, click() to land, enter a text, and press “Enter” to land on a sample page (Google search of testim.io).
  6. At last, use the context_click function to perform the right-click, which opens the context menu.

Note: Selenium can’t control the context menu of the web browser because the context menu is generated by the browser and not by the webpage. Selenium can only control the webpage elements.

Controlling the Context Menu

In order to use the context menu, we’ll use another package named pyautogui. This will help us to move the mouse to the desired location.

Once the mouse is on the desired location and the context menu is open, we can select the preferred option from the list. First, install the package using pip, and then import it in the main.py file.

You can simply import it using the import pyautogui statement. Once done, add the following piece of code at the end of the file:

# Move mouse to Copy Image option using moveTo function
pyautogui.moveTo(X, Y, duration=1)
pyautogui.leftClick()

We’ll use the moveTo function to move the mouse to a specific location. I redacted the coordinates to avoid confusion, as these coordinates vary based on your screen resolution.

You can learn more about pyautogui mouse movements here.

How Do You Move the Mouse to an Element in Selenium?

You can use the Actions class to move the mouse to an element in Selenium. The Actions class has a move_to_element() method, which takes an element as a parameter. This method will move the mouse to the element.

Below is the code snippet to move the mouse to the Pricing anchor tag on testim.io’s homepage and then click on it.

from time import time
from selenium import webdriver
from Selenium.webdriver import ActionChains

 # Main Function

if __name__ == '__main__': 
   # Provide the path of chromedriver present on your system.
   driver = webdriver.Chrome(executable_path="chromedriver")
   action = ActionChains(driver)

   # Provide the URL of the website you want to visit.
   URL = 'https://www.testim.io/'
   driver.get(URL)

   # XPath of Images Hyperlink
   pricing_anchor_xpath = '//a[text()="Pricing"]'
   pricing_element = driver.find_element('xpath', pricing_anchor_xpath)
   action.move_to_element(pricing_element).click().perform()
   action.perform()

   # Sleep for 2 seconds
   time.sleep(2)
   # Close the browser
   driver.close()

How to Perform Double-Click in Selenium

To double-click in Selenium, you can use the Actions class. First, you need to find the element you want to double-click. Then, you can use the double_click() function to perform the double-click.

from selenium import webdriver
from Selenium.webdriver import ActionChains

 # Main Function

if __name__ == '__main__': 

   # Provide the path of chromedriver present on your system.
   driver = webdriver.Chrome(executable_path="chromedriver")
   action = ActionChains(driver)

   # Provide the URL of the website you want to visit.
   URL = 'https://www.testim.io/blog/software-testing-basics/'
   driver.get(URL)

   # XPath of Images Hyperlink
   text_xpath = '//h1[text()="What Is Software Testing? All the Basics You Need to Know"]'
   h1_text = driver.find_element('xpath', text_xpath)

   # Use the double click action on the image tab.
   action.double_click(h1_text).perform()

Conclusion

Right-clicking is a common feature of most web browsers. It can be used to open up a context menu that allows you to do things like open a link, copy a link, or save a link. It’s also a great way to get a link’s URL quickly. 

If you’re looking for a UI testing tool that helps you automate test cases with AI at scale, check out testim.io. With testim.io, you can quickly and easily create AI-powered test cases that are reliable and scalable. Testim.io also offers a wide range of features that make it easy to use and manage your test cases, including a powerful search engine, an intuitive interface, and support for multiple languages.

We hope you enjoyed our post about how to right-click and do other click actions using Selenium. In addition to the other helpful tips, we hope you find this information useful in creating your own automated tests for your software.