Frage

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?

War es hilfreich?

Lösung

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()

Andere Tipps

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!

I encountered the same issue where I had to right-click and click on 'open link in new tab'.

I searched for a lot of answers on google but there was no specific solution I found for Python.

Earlier, I was doing using ActionChains where right-click menu is showing, but then that menu list can't be accessed in selenium as I found some threads saying this has OS-level access.

action = ActionChains(driver)
action.context_click(<obj_which_u_want_to_click>).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform() 

Here, Keys.ARROW_DOWN is not working, and opening the link in the same tab, ideally, it should open in a new tab.

So, there are two ways through which I did this:

First, via send_keys:

link = driver.find_elements_by_xpath("//a[contains(@href, 'https:...')]")
link.send_keys(Keys.CONTROL + Keys.ENTER)

Second, through JavaScript:

driver.execute_script("window.open(arguments[0], '_blank');", link)

I think you can't access the right-click menu items in selenium as it is out of its scope.

You can perform context click using ActionChains, and use Arrows via send_keys to select an element from the context menu.

ActionChains(context.browser).move_to_element(element).context_click(element).perform()
ActionChains(context.browser).send_keys(Keys.ARROW_UP).perform()
ActionChains(context.browser).send_keys(Keys.ENTER).perform()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top