我如何要求Selenium-WebDriver在Java中等待几秒钟?

Pri*_*nce 97 java selenium webdriver selenium-webdriver

我正在研究Java Selenium-WebDriver.我补充道

driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

WebElement textbox = driver.findElement(By.id("textbox"));
Run Code Online (Sandbox Code Playgroud)

因为我的应用程序需要几秒钟来加载用户界面.所以我设置2秒implicitwait.但我无法找到元素文本框

然后我补充说 Thread.sleep(2000);

现在它工作正常.哪一种更好?

eug*_*kov 115

好吧,有两种类型的等待:显式和隐式等待.明确等待的想法是

WebDriverWait.until(condition-that-finds-the-element);
Run Code Online (Sandbox Code Playgroud)

隐式等待的概念是

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Run Code Online (Sandbox Code Playgroud)

你可以在这里获得细节上的不同.

在这种情况下,我更喜欢使用显式等待(fluentWait特别是):

public WebElement fluentWait(final By locator) {
    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(5, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);

    WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
        public WebElement apply(WebDriver driver) {
            return driver.findElement(locator);
        }
    });

    return  foo;
};
Run Code Online (Sandbox Code Playgroud)

fluentWait函数返回找到的Web元素.来自以下文档fluentWait: Wait接口的实现,可以动态配置其超时和轮询间隔.每个FluentWait实例定义等待条件的最大时间量,以及检查条件的频率.此外,用户可以将等待配置为在等待时忽略特定类型的异常,例如在搜索页面上的元素时的NoSuchElementExceptions. 您可以在这里获得详细信息

的使用fluentWait你的情况如下所示:

WebElement textbox = fluentWait(By.id("textbox"));
Run Code Online (Sandbox Code Playgroud)

这种方法恕我直言更好,因为您不知道确切等待多长时间,并且在轮询间隔中您可以设置任意时间值,哪些元素存在将通过验证.问候.

  • 这是`import com.google.common.base.Function;`,而不是`import java.util.function.Function;` (2认同)
  • 现在是:`.withTimeout(Duration.ofSeconds(30)).pollingEvery(Duration.ofSeconds(5))`而不是:`.withTimeout(30, TimeUnit.SECONDS).pollingEvery(5, TimeUnit.SECONDS)` (2认同)

Anu*_*nup 15

如果使用webdriverJs(node.js),

driver.findElement(webdriver.By.name('btnCalculate')).click().then(function() {
    driver.sleep(5000);
});
Run Code Online (Sandbox Code Playgroud)

上面的代码使浏览器在单击按钮后等待5秒钟.

  • 当问题专门针对Java而不是节点/ JavaScript时,为什么要发布这个?这是关于如何在ruby中做到这一点的主题. (17认同)
  • @Thor84no 主要是因为我们中的一些人在搜索网络解决方案时找到了这个答案 (2认同)

小智 15

这个帖子有点老了,但我想我会发布我目前正在做的事情(正在进行中).

虽然我仍然遇到系统负载很重的情况,但当我点击提交按钮(例如,login.jsp)时,所有三个条件(见下文)都会返回,true但是下一页(例如,home.jsp)还没有我开始加载了.

这是一个通用的等待方法,它采用ExpectedConditions列表.

public boolean waitForPageLoad(int waitTimeInSec, ExpectedCondition<Boolean>... conditions) {
    boolean isLoaded = false;
    Wait<WebDriver> wait = new FluentWait<>(driver)
            .withTimeout(waitTimeInSec, TimeUnit.SECONDS)
            .ignoring(StaleElementReferenceException.class)
            .pollingEvery(2, TimeUnit.SECONDS);
    for (ExpectedCondition<Boolean> condition : conditions) {
        isLoaded = wait.until(condition);
        if (isLoaded == false) {
            //Stop checking on first condition returning false.
            break;
        }
    }
    return isLoaded;
}
Run Code Online (Sandbox Code Playgroud)

我已经定义了各种可重复使用的ExpectedConditions(下面有三个).在此示例中,三个预期条件包括document.readyState ='complete',不存在"wait_dialog",并且没有'spinners'(表示正在请求异步数据的元素).

只有第一个可以通用地应用于所有网页.

/**
 * Returns 'true' if the value of the 'window.document.readyState' via
 * JavaScript is 'complete'
 */
public static final ExpectedCondition<Boolean> EXPECT_DOC_READY_STATE = new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver driver) {
        String script = "if (typeof window != 'undefined' && window.document) { return window.document.readyState; } else { return 'notready'; }";
        Boolean result;
        try {
            result = ((JavascriptExecutor) driver).executeScript(script).equals("complete");
        } catch (Exception ex) {
            result = Boolean.FALSE;
        }
        return result;
    }
};
/**
 * Returns 'true' if there is no 'wait_dialog' element present on the page.
 */
public static final ExpectedCondition<Boolean> EXPECT_NOT_WAITING = new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver driver) {
        Boolean loaded = true;
        try {
            WebElement wait = driver.findElement(By.id("F"));
            if (wait.isDisplayed()) {
                loaded = false;
            }
        } catch (StaleElementReferenceException serex) {
            loaded = false;
        } catch (NoSuchElementException nseex) {
            loaded = true;
        } catch (Exception ex) {
            loaded = false;
            System.out.println("EXPECTED_NOT_WAITING: UNEXPECTED EXCEPTION: " + ex.getMessage());
        }
        return loaded;
    }
};
/**
 * Returns true if there are no elements with the 'spinner' class name.
 */
public static final ExpectedCondition<Boolean> EXPECT_NO_SPINNERS = new ExpectedCondition<Boolean>() {
    @Override
    public Boolean apply(WebDriver driver) {
        Boolean loaded = true;
        try {
        List<WebElement> spinners = driver.findElements(By.className("spinner"));
        for (WebElement spinner : spinners) {
            if (spinner.isDisplayed()) {
                loaded = false;
                break;
            }
        }
        }catch (Exception ex) {
            loaded = false;
        }
        return loaded;
    }
};
Run Code Online (Sandbox Code Playgroud)

根据页面的不同,我可以使用其中一个或全部:

waitForPageLoad(timeoutInSec,
            EXPECT_DOC_READY_STATE,
            EXPECT_NOT_WAITING,
            EXPECT_NO_SPINNERS
    );
Run Code Online (Sandbox Code Playgroud)

以下类中还预定义了ExpectedCondition: org.openqa.selenium.support.ui.ExpectedConditions


Val*_*tin 9

使用Thread.sleep(2000);是无条件的等待.如果你的测试加载速度更快,你仍然需要等待.所以原则上使用implicitlyWait是更好的解决方案.

但是,我不明白为什么implicitlyWait在你的情况下不起作用.你是否测量findElement过在抛出异常之前实际需要两秒钟.如果是这样,你可以尝试使用WebDriver的条件等待,如答案所述?