【问题标题】:How to Navigate Context Menus (Selenium, Python)如何导航上下文菜单(Selenium、Python)
【发布时间】:2017-08-21 04:10:14
【问题描述】:

我知道 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()

上下文:

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

研究:

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

Select an Option from the Right-Click Menu in Selenium Webdriver - Java - 线程建议箭头键命令应该工作。但是,所有示例都使用 Java 和 .build() 命令

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

How to perform right click using Selenium ChromeDriver? - 与我的问题非常相似。虽然一位用户建议无法与菜单进行交互,但另一位用户建议 actionChains 变通方法可以工作。

【问题讨论】:

  • 你能向我们展示你的工作/研究吗?
  • @DebanjanB 已编辑。谢谢
  • 正如这里所解释的 - elementalselenium.com/tips/63-right-clickFurther - 右键菜单通常是 selenium 无法触及的系统级菜单。更多讨论:github.com/GoogleChrome/puppeteer/issues/1575
  • 你能找到一个只用硒的解决方案吗?我陷入了类似的境地。
  • @Ontropy 我上一次做这个是几年前的事了。但是据我记得,其他评论者/答案是正确的。 Selenium 仅与网页交互,仅此而已。它甚至不能与浏览器的 UI(如后退按钮)或操作系统级别的 UI(如上下文菜单)交互。出于这个原因,您应该查看其他工具来完成导航上下文菜单。像其他答案中提到的那样,存在解决方法。但是,这些取决于操作系统。

标签: python selenium


【解决方案1】:

薄,

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

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

【讨论】:

  • +1 为答案。我已经尝试过这种解决方法并且它有效。虽然它是系统相关的(仅限 Windows 的解决方案)并且有问题(不能在无头模式下工作,浏览器应该在前台,也许等等),它应该是操作上下文菜单的接受答案。
【解决方案2】:

我们可以使用 selenium 和 pyautogui 来解决这个问题。使用 pyautogui 的原因是我们需要控制鼠标来控制上下文菜单上的选项。为了演示这一点,我将使用 python 代码在新标签页中自动打开复仇者联盟残局的谷歌图像。

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

现在在上面的代码中,直到 pyautogui.moveTo(120, 130, duration=1) 的部分是基于硒的。您的答案从 pyautogui.moveTo(120, 130, duration=1) 开始,这只是将鼠标按钮移动到上下文菜单的 open image in new tab 选项(请注意屏幕坐标可能因您的屏幕尺寸而异)。下一行单击该选项(使用 action.click().perform() 将无法按预期工作)。

接下来的两行有助于在标签打开后导航到标签。希望代码有所帮助!

【讨论】:

    【解决方案3】:

    我认为至少在 java 中解决此问题的一种方法是使用 Robot 类的一个实例来执行这些操作。

    这只是在 chromme 中处理身份验证的一个示例。当我需要在 selenium 范围之外执行操作时非常有用。

    try{
            Robot bot = new Robot();
    
            for(char c: userArray){
                bot.keyPress(c);
                bot.keyRelease(c);
                bot.delay(300);
            }
            bot.keyPress(KeyEvent.VK_TAB);
            bot.keyRelease(KeyEvent.VK_TAB);
    
            for(char c: userArray){
                bot.keyPress(c);
                bot.keyRelease(c);
                bot.delay(300);
            }
    
            bot.keyPress(KeyEvent.VK_ENTER);
            bot.keyRelease(KeyEvent.VK_ENTER);
    
        }catch(Exception e){
            e.printStackTrace();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      相关资源
      最近更新 更多