如何导航上下文菜单(Selenium、Python)

Whe*_*hin 5 python selenium

我知道 Selenium 显然不支持导航上下文菜单。但我也在其他几个线程中看到,使用动作链可以解决问题。使用context_click()后跟箭头键命令在菜单中导航。

我见过的所有例子都使用了 Java,当我翻译成 Python 时,只有context_click()命令会注册。奇怪的是,我也不会出错。其他消息来源表示,Selenium 生成的上下文菜单只是系统级别的,因此,Selenium 无法触及它们,只能创建。

所以我的问题是,有没有人能够通过 Selenium 从上下文菜单中成功导航和选择选项?Python 示例是首选,但我会接受我能得到的任何建议或答案。

编辑:

代码:

driver.get('https://www.google.com/')
actionChains = ActionChains(driver)
actionChains.context_click().send_keys(Keys.ARROW_UP).send_keys(Keys.ENTER).perform()
Run Code Online (Sandbox Code Playgroud)

语境:

这只是我一直在运行以测试这种情况的测试脚本。在我的个人项目中,我需要导航上下文菜单以访问 chrome 扩展。由于 selenium 只能在网页内进行交互,因此我无法让它点击浏览器显示的 Chrome 扩展程序的按钮。所以这是我一直在尝试的解决方法。

研究:

https://testingrepository.com/how-to-right-click-using-selenium-webdriver/ - 这个来源告诉 seleniums 上下文菜单只是系统级别。在 Java 示例中,它们也使用.build()命令。据我所知,此命令不适用于 Python。

从 Selenium Webdriver - Java - Thread的右键单击菜单中选择一个选项,建议箭头键命令应该起作用。但是,所有示例都使用 Java 和.build()命令

https://github.com/SeleniumHQ/selenium/blob/master/py/selenium/webdriver/common/action_chains.py - 显示ActionChains()Python 版本的.build()命令。对某些人来说可能是常识。我之前不知道这个。

如何使用 Selenium ChromeDriver 执行右键单击? - 与我的问题非常相似。虽然一位用户建议无法与菜单交互,但另一位用户建议 actionChains 解决方法将起作用。

小智 6

薄的,

我遇到了同样的问题,想知道没有人已经回答过这个问题......我无法用硒解决它,因为硒会在页面内导航。我的解决方案:

import win32com.client as comclt
wsh= comclt.Dispatch("WScript.Shell")
ActionChains(driver).move_to_element(element).context_click().perform()
wsh.SendKeys("{DOWN}") # send the keys you want
Run Code Online (Sandbox Code Playgroud)