【问题标题】:How to hover over (mouseover) an element in Selenium Ruby?如何将鼠标悬停在 Selenium Ruby 中的元素上(鼠标悬停)?
【发布时间】:2011-11-08 01:58:09
【问题描述】:

有人知道如何将鼠标悬停在 Selenium Ruby Webdriver 中的元素上吗?

我的代码是这样的:

el = driver.find_element(:css => "#foo")
driver.move_to el # How do I trigger a mouseover event on this element?

我在 Linux 32 位的 Firefox 中使用 selenium-webdriver gem。

【问题讨论】:

    标签: ruby selenium automation


    【解决方案1】:

    我使用了driver.action.move_to(el).perform,它与其他答案略有不同,所以我想为了完整起见我会包括它。

    【讨论】:

    • 目前 selenium 已经分叉了move_to 功能,所以你现在需要使用.action 来访问鼠标功能。
    • @DanSabin 你想提出一个澄清这一点的编辑吗?我会完全接受。
    【解决方案2】:

    原来答案是:

    driver.move_to(el).perform
    

    我忘记了.perform

    【讨论】:

    • 检查@Seanny123 和我的评论,了解为什么现在可以使用。
    【解决方案3】:

    这对我有用:

    driver.mouse.move_to el
    

    【讨论】:

    • 检查@Seanny123 和我在下面关于为什么现在可以工作的答案的评论。
    【解决方案4】:

    您需要使用 Selenium's Action Builder 来访问更复杂的操作,例如悬停(这是 seanny123 的答案所展示的)。

    此外,如果您正在使用悬停,您可能需要动态等待它显示,然后再执行下一步操作(例如,使用显式等待)。

    我整理了一个关于如何做到这一点的示例——您可以查看完整的文章here

    【讨论】:

      【解决方案5】:

      悬停元素:

      driver.action.move_to(element).perform
      # e.g.    
      driver.action.move_to(driver.find_element(css: 'a')).perform
      

      将元素悬停在特定位置:

      driver.action.move_to(element, mouse_x, mouse_y).perform
      # e.g.    
      driver.action.move_to(driver.find_element(css: 'a'), 100, 100).perform
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-03
        • 2021-07-15
        • 1970-01-01
        • 1970-01-01
        • 2011-06-02
        • 1970-01-01
        • 2021-11-10
        • 2011-08-04
        相关资源
        最近更新 更多