How to perform right click using Selenium ChromeDr

2019-04-05 06:38发布

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?

2条回答
【Aperson】
2楼-- · 2019-04-05 07:19

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()
查看更多
小情绪 Triste *
3楼-- · 2019-04-05 07:33

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()
查看更多
登录 后发表回答