【问题标题】:Selenium Wait for element works only when debug selected testSelenium 等待元素仅在调试选定的测试时起作用
【发布时间】:2015-07-16 05:30:43
【问题描述】:

我有使用 ajax 构建的网页。 代码中有一个方法:

public static void ProceedButtonClick()
{
    Driver.WaitForAndIsEnabledFind(ProceedButtonElementBy).Click();
    Driver.WaitFor(LoadedPageFinishedIdentifierBy);
}

如您所见,我只单击并稍后等待下一个窗口显示。 当我通过“运行选定的测试”运行此代码时,测试在等待时冻结并超时。但是该窗口在页面上可见。我可以在浏览器中看到它。 如果我通过“调试选定的测试”运行此代码,一切正常。我什至不需要设置断点。调试模式就足够了。选择器没问题。我仔细检查了它们。 这是我的 WaitFor 方法:

public static void ProceedButtonClick()
{
    Driver.WaitForAndIsEnabledFind(ProceedButtonElementBy).Click();
    Driver.WaitFor(LoadedPageFinishedIdentifierBy);
}

【问题讨论】:

    标签: c# selenium testing automated-tests selenium-chromedriver


    【解决方案1】:

    始终尝试使用以下方法进行显式等待,这将每 3 秒检查一次元素并单击。

    方法:

      public static void waitForElementToBeVisible(final WebDriver driver,
            final By locator) throws RuntimeException {
        Wait<WebDriver> wait = new WebDriverWait(driver, WAIT_TIMEOUT);
        try {
            wait.until(new ExpectedCondition<WebElement>() {
                public WebElement apply(WebDriver driver) {
                    // driver.switchTo().defaultContent();
                    WebElement element = driver.findElement(locator);
                    if (element.isDisplayed()) {
                        return element;
                    }
                    return null;
                }
            });
        } catch (Exception e) {
            throw new RuntimeException("Exception while waiting for " + locator
                    + ". Exception:" + e + " on " + driver.getCurrentUrl());
        }
    }
    

    【讨论】:

      【解决方案2】:

      在查找元素之前使用此代码:

      try {
              Thread.sleep(10000);
          } catch(InterruptedException ex) {
              Thread.currentThread().interrupt();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        • 2021-07-28
        相关资源
        最近更新 更多