【问题标题】:How to mouseover on a hyperlink - Webdriver如何将鼠标悬停在超链接上 - Webdriver
【发布时间】:2013-08-01 05:07:44
【问题描述】:

我正在为我的项目使用 Selenium Webdriver。我已经自动化了代码以将鼠标悬停在图像上,这已成功完成。但是,有些我无法使用此代码将鼠标悬停在超链接上。

我使用的代码是

Actions build1 = new Actions(driver); build1.moveToElement(WebElement).build().perform();

我也尝试过使用

Locatable hoverItem = (Locatable) driver.findElement(); Mouse mouse = ((HasInputDevices) driver).getMouse(); mouse.mouseMove(hoverItem.getCoordinates())

但这也行不通。请帮我解决这个问题

【问题讨论】:

    标签: javascript selenium selenium-webdriver mouseover


    【解决方案1】:

    我遇到了同样的问题并通过将光标移动 1px 来解决它。最后一行触发了悬停事件。

    Actions action = new Actions(driver);
    action.moveToElement(element).build().perform();
    action.moveByOffset(1, 1).build().perform();
    

    【讨论】:

      【解决方案2】:

      试试这个:

      Actions action = new Actions(webdriver);
      WebElement we = webdriver.findElement(By.xpath("x_p_a_t_h"));
      action.moveToElement(we).build().perform();
      

      【讨论】:

      • 这和他最初的Actions build1 = new Actions(driver); build1.moveToElement(WebElement).build().perform();有什么不同?
      【解决方案3】:

      我在DefaultSelenium 类中使用了public void mouseOver(String) 方法。代码实质如下:

      protected void hoverAction() {
          WebDriverBackedSelenium webDriver = some_elaborate_method_to_get_webdriver;
          webDriver.mouseOver("x_p_a_t_h");
      }
      

      您可能还需要考虑在悬停时设置某种等待时间,以确保在您失败之前渲染元素(例如,飞出菜单,通常从链接启动不会立即出现)。

      【讨论】:

      • @SudharsanSrinivasan 也许您需要确保元素已加载。在我的实现中(我们已经对 WebDriver 进行了相当精细的扩展以获取 Singleton 实例),我们仅在加载元素后才使用 hoverAction。在使用 FirefoxDriver 时,我在这方面没有任何测试失败..
      • 是的,该元素已加载。我可以点击超链接。但无法将鼠标悬停在它上面。
      【解决方案4】:

      所有其他发帖者都张贴了我能提出的所有建议,但似乎都没有奏效。当我到达这一点时,我退后一步,问为什么我需要将鼠标悬停在超链接上。只是检查替代文本吗?如果是这种情况,我会使用 element.getAttribute("alt") 并验证文本是否符合我的预期。我不需要测试浏览器悬停功能。我唯一可以建议的另一件事是确保在运行测试时鼠标光标不在浏览器窗口上。这也可以摆脱鼠标悬停。

      【讨论】:

      • 感谢您的回复。当鼠标悬停在超链接上时,我需要获取超链接的颜色。关于第二点,是的。运行测试时光标不在浏览器窗口上。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2012-04-08
      • 2017-03-06
      • 2013-10-12
      • 2015-08-20
      • 2016-11-06
      • 1970-01-01
      相关资源
      最近更新 更多