Selenium 驱动程序 (Chrome) 无法在悬停元素上找到下拉列表

Selenium Driver (Chrome) Can't Find Dropdown on hover Element

我正在使用 Selenium Chrome 驱动程序 运行 在各种站点环境中进行一些测试,但是,当我尝试使用悬停下拉菜单中的元素时,我不能似乎可靠 select 元素。当我调试时,这 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,但不幸的是,下拉列表的元素实际上并不是 link 我必须将鼠标悬停在上方才能打开下拉列表的子元素。如果我尝试使用 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();

查看此问题了解更多详情: