我正在使用 Selenium Chrome 驱动程序在各种站点环境中运行一些测试,但是,当尝试使用悬停下拉菜单中的元素时,我似乎无法可靠地选择这些元素。当我调试时,它 100% 有效,但是当我在没有附加调试器的情况下运行它时,大约 2/3 的时间会失败。这是代码:
private void prepWindow(WebDriver driver, boolean isNightly, String toClick) {
WebDriverWait wait = new WebDriverWait(driver, 300);
try {
if (isNightly) {
WebElement nightlyPopup = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(BOWebElements.nightlyPopup)));
nightlyPopup.click();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Nightly popup has changed names again.", "Error", JOptionPane.ERROR_MESSAGE);
}
WebElement user = wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Users")));
Actions action = new Actions(driver);
action.moveToElement(user).build().perform(); //Makes hover drop down appear
driver.findElement(By.id(toClick)).click(); //Should click element that is only visible when hover drop down is open
}
我还应该注意到,上面的相同代码无需在同事的计算机上使用调试器即可完美运行,但在我自己的计算机上则不然。
我想使用 XPath,但不幸的是,下拉列表的元素实际上并不是我必须将鼠标悬停在链接上才能打开下拉列表的子元素。如果我尝试使用 XPath 直接导航到该元素,则会出现错误,指出 XPath 无效。这是潜在的 XPath 之一:
//html/body/#outTemplateId/#preambleFormId/#globalNavigation/#navBGC/#navBGCmainMM/ul/li/ul/table/tbody/tr/td/ul.ui-menu-list.ui-helper-reset/li.ui-menuitem.ui-widget.ui-corner-all/a#fleetUsersId2.ui-menuitem-link.ui-corner-all.submenu
如何使行为一致?
最佳答案
将您的操作链接在一起以更好地模拟用户将采取的操作:
action.moveToElement(user).moveToElement(driver.findElement(By.id(toClick))).click().build().perform();
查看此问题了解更多详细信息: https://stackoverflow.com/a/17294390/3537915
关于java - Selenium Webdriver (Chrome) 无法在悬停元素上找到下拉菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39211687/