Python 如何使用 Selenium ChromeDriver 执行右键单击?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20316864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to perform right click using Selenium ChromeDriver?
提问by CamIce
I have been searching for this a lot, but could not find an answer for Python.
我一直在寻找这个很多,但找不到 Python 的答案。
Is it possible to simulate right click, or open up the context menu via selenium/chromedriver?
是否可以通过 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?
我见过 Java 和其他一些语言的选项,但从未见过 Python。我需要做什么来模拟对链接或图片的右键单击?
采纳答案by Yi Zeng
It's called context_clickin 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.
它context_click在selenium.webdriver.common.action_chains 中调用。请注意,Selenium 无法对浏览器级别的上下文菜单执行任何操作,因此我假设您的链接会弹出 HTML 上下文菜单。
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
actionChains = ActionChains(driver)
actionChains.context_click(your_link).perform()
回答by Konstantin Yakovskyi
You can perform context click using ActionChains, and use Arrows via send_keys to select an element from the context menu.
您可以使用 ActionChains 执行上下文单击,并通过 send_keys 使用箭头从上下文菜单中选择一个元素。
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()
回答by Prat9501
I encountered to the same issue where I had to right click and click on 'open link in new tab'. I searched for 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.
我遇到了同样的问题,我必须右键单击并单击“在新选项卡中打开链接”。我在谷歌上搜索了很多答案,但没有找到针对 python 的特定解决方案。早些时候,我正在使用显示右键菜单的“ActionChains”,但随后无法在 selenium 中访问该菜单列表,因为我发现一些线程说这具有操作系统级别的访问权限。
action = ActionChains(driver) action.context_click().send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
action = ActionChains(driver) action.context_click().send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
Here, Keys.ARROW_DOWN is not working and opening the link in same tab, ideally it should open in new tab. So, there are two ways through which i done this:
在这里, Keys.ARROW_DOWN 不起作用并在同一个选项卡中打开链接,理想情况下它应该在新选项卡中打开。所以,我有两种方法可以做到这一点:
link = driver.find_elements_by_xpath("//a[contains(@href, 'https:...')]")
link.send_keys(Keys.CONTROL + Keys.ENTER)
Through javascript..
driver.execute_script("window.open(arguments[0], '_blank');", link)
link = driver.find_elements_by_xpath("//a[contains(@href, 'https:...')]")
link.send_keys(Keys.CONTROL + Keys.ENTER)
通过javascript..
driver.execute_script("window.open(arguments[0], '_blank');", link)
What i think you can't access the right click menu items in selenium as it is out of it's scope.
我认为您无法访问 selenium 中的右键单击菜单项,因为它超出了它的范围。

