Right Click with ActionChains

In previous section, we have discussed about the ActionChains which helps us to perform Drag  & Drop operation. Now we will see how to perform Right Click in Selenium Python.

Objective:

  • Introduction
  • Test Scenario
  • Right Click in Selenium Python with Example

Introduction:

We are using right click very often. Let’s say there is an element and you need to right click and choose an option to perform some operation. It may be edit, open in new window etc. In some other case if you click on any blank space in web page then it will open a list which is browser option like back, forward, Reload. But we cannot inspect this. But if you click on any link, button or any other element in application then it will open a list with application option which we will automate now.

So how to perform Right click and how to handle this scenario? 

In Selenium, instead of Right click, we have Context click.

Test Scenario:

For this we will navigate to website (https://swisnl.github.io/jQuery-contextMenu/demo.html)to learn Right Click / Context Click.

Right Click in Selenium Python with Example:

Let’s have a look on below example for understanding the Selenium Python.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://swisnl.github.io/jQuery-contextMenu/demo.html')
driver.maximize_window()
time.sleep(10)

right_clcik = driver.find_element(By.XPATH, "//span[text()='right click me']")

act_chain = ActionChains(driver)
act_chain.context_click(right_clcik).perform()

Output:

Explanation:

From the above example you can notice that we have imported ActionChains class which helps us to do Right Click or Context Click. To work with ActionChains we first we need to find the web element to which we want to click right click. Then we will create the the ActionChains object by providing the driver object as argument to the ActionChains(). After we created the ActionChains object we can call the context_click and pass element on which we want perform right click. After all these, we have to call perform() function at last which is mandatory. These 3 steps will execute our Right Click on Selenium Python.

Now let’s click on “copy” option. To perform this, you need to find the element for the “Copy” element. And click on the element.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://swisnl.github.io/jQuery-contextMenu/demo.html')
driver.maximize_window()
time.sleep(10)

right_clcik = driver.find_element(By.XPATH, "//span[text()='right click me']")
copy_clcik = driver.find_element(By.XPATH, "//span[text()='Copy']")

act_chain = ActionChains(driver)
act_chain.context_click(right_clcik).perform()
copy_clcik.click()

Summary:

  • We are using right click very often. Let’s say there is an element and you need to right click and choose an option to perform some operation.
  • If you click on any blank space in web page then it will open a list which is browser option like back, forward, Reload. But we cannot inspect this. But if you click on any link, button or any other element in application then it will open a list with application option.
  • To work with ActionChains we first we need to find the web element to which we want to click right click. Then we will create the the ActionChains object by providing the driver object as argument to the ActionChains().
  • After we created the ActionChains object we can call the context_click and pass element on which we want perform right click. After all these, we have to call perform() function at last which is mandatory

Now we are clear about the Right click and which right click we should be able to automate in Selenium. Addition to these, we understood how to automate the right click in Selenium Python. Next section we will discuss more about advance ways for “send Keys” to element

Leave a Reply

Your email address will not be published. Required fields are marked *