如何在超链接上鼠标悬停-Webdriver

How to mouseover on a hyperlink - Webdriver

本文关键字:鼠标 悬停 -Webdriver 超链接      更新时间:2023-09-26

我的项目使用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())

但它也不起作用。请帮我处理这个

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

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

试试这个:

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

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

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

你可能还需要考虑在悬停时留出一些等待时间,以确保元素在失败之前得到渲染(例如,通常从链接启动的弹出菜单不会立即出现)。

所有其他海报都发布了我能想到的所有建议,但似乎都不起作用。当我谈到这一点时,我后退一步,问为什么我需要悬停在超链接上。只是为了检查替换文本吗?如果是这样的话,我会使用element.getAttribute("alt")并验证文本是否符合我的预期。我不需要测试浏览器悬停功能。我唯一能建议的是,在运行测试时,确保鼠标光标不在浏览器窗口上。这也可以让鼠标悬停。