Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform right click using Selenium ChromeDriver?

Tags:

I have been searching for this a lot, but could not find an answer for Python.

Is it possible to simulate right click, or open up the context menu via selenium/chromedriver?

I have seen options for Java, and some other languages, but never in Python. What would I have to do to simulate a right click on a link, or a picture?

like image 996
CamIce Avatar asked Dec 01 '13 20:12

CamIce


People also ask

How will you perform right click?

How to right-click on a laptop. On a laptop, if there are two buttons below the touchpad, pressing the right button will execute the right-click action. If there are no buttons below the touchpad, press the bottom right of the touchpad to perform the right-click action.

Which class is used for right click in Selenium Webdriver?

For right clicking an element in Selenium, we make use of the Actions class. The Actions class provided by Selenium Webdriver is used to generate complex user gestures including right click, double click, drag and drop etc.

How many ways we can click button in Selenium?

Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method. The parameters – arguments[0]. click() and locator of the element on which the click is to be performed are passed to this method.


2 Answers

It's called context_click in selenium.webdriver.common.action_chains. Note that Selenium can't do anything about browser level context menu, so I assume your link will pop up HTML context menu.

from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
actionChains = ActionChains(driver)

actionChains.context_click(your_link).perform()
like image 172
Yi Zeng Avatar answered Oct 04 '22 21:10

Yi Zeng


To move through the context menu we have to use pyautogui along with selenium. The reason for using pyautogui is that we need to have control of the mouse for controlling the options on the context menu. To demonstrate this, I am going to use a python code to automatically open a google image of Avengers Endgame in new tab.

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
import pyautogui

URL = 'https://www.google.com/'
PATH = r'C:\Program Files (x86)\chromedriver.exe'

driver = webdriver.Chrome(PATH)
action = ActionChains(driver)
driver.get(URL)

search = driver.find_element_by_name('q')
search.send_keys('Avengers Endgame')
search.send_keys(Keys.RETURN)

image_tab = driver.find_element_by_xpath('//a[text()="Images"]')
image_tab.click()

required_image = driver.find_element_by_xpath('//a[@class="wXeWr islib nfEiy mM5pbd"]')
action.context_click(required_image).perform()
pyautogui.moveTo(120, 130, duration=1)
pyautogui.leftClick()
time.sleep(1)
pyautogui.moveTo(300,40)
pyautogui.leftClick()

Now in the above code, the part till pyautogui.moveTo(120, 130, duration=1) is selenium based. Your answer begins from pyautogui.moveTo(120, 130, duration=1) and what this does is simply moves the mouse button to the open image in new tab option of the context menu(Please note that the screen coordinates may vary based on your screen size). The next line clicks the option(using action.click().perform() won't work as expected).

The next two lines helps in navigating to the tab after it opens. Hope the code helps!

like image 30
Aknk Avatar answered Oct 04 '22 21:10

Aknk