【问题标题】:How to perform right click using Selenium ChromeDriver?如何使用 Selenium ChromeDriver 执行右键单击?
【发布时间】:2013-12-17 11:40:14
【问题描述】:

我一直在寻找这个,但找不到 Python 的答案。

是否可以模拟右键单击,或者通过 selenium/chromedriver 打开上下文菜单?

我见过 Java 和其他一些语言的选项,但从未见过 Python。 我需要做什么来模拟右键单击链接或图片?

【问题讨论】:

    标签: python selenium webdriver selenium-webdriver selenium-chromedriver


    【解决方案1】:

    selenium.webdriver.common.action_chains 中称为context_click。请注意,Selenium 不能对浏览器级别的上下文菜单做任何事情,所以我假设您的链接会弹出 HTML 上下文菜单。

    from selenium import webdriver
    from selenium.webdriver import ActionChains
    
    driver = webdriver.Chrome()
    actionChains = ActionChains(driver)
    
    actionChains.context_click(your_link).perform()
    

    【讨论】:

    • 感谢您的回复。您是否知道任何可以让我打开浏览器上下文菜单(使用查看源代码、检查元素等)的信息?我有一个扩展,我需要在其中标记解决方案的验证码和输入(插件名为 SkipInput 或 Rumola)。你知道我怎样才能让它工作吗?
    • actionChains.context_click().perform() 应该可以。但是最终你不能对菜单做任何事情,这超出了 Selenium 的范围。
    【解决方案2】:

    要在上下文菜单中移动,我们必须使用 pyautogui 和 selenium。使用 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】:

      我遇到了同样的问题,我必须右键单击并单击“在新选项卡中打开链接”。

      我在谷歌上搜索了很多答案,但没有找到针对 Python 的具体解决方案。

      之前,我使用ActionChains 显示右键菜单,但后来无法在 selenium 中访问该菜单列表,因为我发现一些线程说它具有操作系统级别的访问权限。

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

      这里,Keys.ARROW_DOWN 不起作用,在同一个标​​签中打开链接,理想情况下,它应该在新标签中打开。

      所以,我有两种方法:

      首先,通过send_keys

      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)
      

      我认为你无法访问 selenium 中的右键菜单项,因为它超出了它的范围。

      【讨论】:

        【解决方案4】:

        您可以使用 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()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-20
          • 2019-01-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多