Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Java的Selenium WebDriver中的moveToElement鼠标悬停函数不稳定_Java_Selenium_Selenium Webdriver - Fatal编程技术网

使用Java的Selenium WebDriver中的moveToElement鼠标悬停函数不稳定

使用Java的Selenium WebDriver中的moveToElement鼠标悬停函数不稳定,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我使用Selenium 3.0.1来运行使用TestNG的自动化测试。 在一个测试中,我试图将鼠标悬停在操作菜单上,然后单击该菜单中的一个选项: Actions builder = new Actions(getWebDriver()); builder.moveToElement(actionButton).build().perform(); 但测试并不稳定。我可以看到菜单打开但立即关闭,因此测试失败,因为它再也找不到选项。 我收到这个错误: java.lang.IllegalArgume

我使用Selenium 3.0.1来运行使用TestNG的自动化测试。 在一个测试中,我试图将鼠标悬停在操作菜单上,然后单击该菜单中的一个选项:

Actions builder = new Actions(getWebDriver());
builder.moveToElement(actionButton).build().perform();
但测试并不稳定。我可以看到菜单打开但立即关闭,因此测试失败,因为它再也找不到选项。 我收到这个错误:

java.lang.IllegalArgumentException: Must provide a location for a move action.
at org.openqa.selenium.interactions.MoveMouseAction.<init>(MoveMouseAction.java:30)
at org.openqa.selenium.interactions.Actions.moveToElement(Actions.java:251)
当我们将鼠标悬停在菜单上时,它是这样的:

我发现这个问题:
这最能解释我的问题。不幸的是,仍然没有解决办法

您可以使用
FluentWait
等待菜单在悬停后出现,如下所示:

FluentWait<> wait = new FluentWait<>(getWebDriver())
            .withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(StaleElementReferenceException.class)
            .ignoring(NoSuchElementException.class)
            .ignoring(ElementNotVisibleException.class)

wait.until(x -> { return driver.findElement(menuElementBy); } );
FluentWait wait=newfluentwait(getWebDriver())
.withTimeout(驱动器超时秒,时间单位秒)
.pollingEvery(500,时间单位为毫秒)
.忽略(StaleElementReferenceException.class)
.忽略(NoSuchElementException.class)
.忽略(ElementNotVisibleException.class)
wait.until(x->{return driver.findElement(menuElementBy);});

如果鼠标悬停成功-菜单开始出现-没有理由需要调用它两次。

这似乎是一个计时问题

如果菜单具有过渡效果,则添加效果持续时间的延迟:

new Actions(driver)
  .moveToElement(menu)
  .pause(100) // duration of the transition effect in ms
  .perform();

submenu.click();

您还可以等待目标元素变得可见和稳定(同一位置连续返回两次)。

如果不需要打开菜单,请尝试使用JavascriptExecutor单击该选项。JavascriptExecutor也可以单击隐藏的元素,使用JavascriptExecutor触发单击所需的全部条件是该元素存在于DOM中

代码段(Java):


这里的问题是,菜单确实会打开,但在Selenium找到子菜单按钮之前会立即关闭。我认为FluentWait在这里不会有帮助,因为菜单已经关闭,所以它将不等待任何东西。如果我错了,请纠正我。我正在寻找的是一种在下拉菜单打开后冻结它的方法,但不点击它。如果没有看到漏洞场景和网站本身,很难说你是否错了。。。所以我鼓励您先尝试等待。它是否也适用于JavascriptExecutor?((JavascriptExecutor)驱动程序).executeScript($('element_selector').hover();”;
new Actions(driver)
  .moveToElement(menu)
  .pause(100) // duration of the transition effect in ms
  .perform();

submenu.click();
((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.cssSelector("hiddenOptionFromMenu")));