2016-11-23 199 views
0

我使用Selenium 3.0.1运行使用TestNG的自动化测试。 在一次测试中我试图悬停动作菜单,然后单击该菜单中的选项:moveToElement鼠标悬停功能在Selenium WebDriver中使用Java不稳定

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) 

我如何检查菜单打开? perform()方法返回void。 我注意到如果我把moveToElement调用两次,比测试更稳定。有没有这样做的优雅选择?

Actions builder = new Actions(getWebDriver()); 
builder.moveToElement(actionButton).build().perform(); 
builder.moveToElement(actionButton).build().perform(); 

这菜单看起来如何,当我们将鼠标悬停在它像: hover over menu

我发现这个问题: https://sqa.stackexchange.com/questions/3467/issue-with-losing-focus-of-hover-command-when-the-mouse-is-outside-of-the-acti 这也解释了最好的我的问题。不幸的是,仍然没有解决方案。

回答

1

如果您没有必要打开菜单,请尝试使用JavascriptExecutor单击该选项。 JavascriptExecutor也可以单击一个隐藏元素,使用JavascriptExecutor触发点击所需的全部内容就是该元素存在于DOM中。

片段(JAVA):

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

您可以等待菜单悬停后出现了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); }); 

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

+0

这里的问题是,菜单不打开,但关闭之前硒能找到的子菜单按钮。我觉得FluentWait在这里不会有帮助,因为菜单已经关闭了,所以它只会等待一切。如果我错了,请纠正我。 我正在寻找的是一种在打开后冻结下拉菜单但不点击它的方法。 – sami610

+0

很难说如果你的错误没有看到洞情节和网站本身......所以我会鼓励你先尝试等待。 – Moshisho

+0

它是否也与JavaScriptExecutor一起使用? ((JavascriptExecutor)driver).executeScript(“$('element_selector')。hover();”); –

0

这似乎是一个时间问题。

如果菜单有一个过渡效果,然后添加效果的持续时间的延迟:

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

submenu.click(); 

您也可以等待目标元素变得可见,稳定(同一位置的返回两次行)。

+0

也没有工作。 – sami610

相关问题