Selenium WebDriver:流畅的等待按预期工作,但隐式等待没有 [英] Selenium WebDriver: Fluent wait works as expected, but implicit wait does not

查看:16
本文介绍了Selenium WebDriver:流畅的等待按预期工作,但隐式等待没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Selenium WebDriver 的新手,正在尝试了解等待"元素出现的正确方法.

I am new to Selenium WebDriver and am trying to understand the correct way to 'wait' for elements to be present.

我正在测试一个页面,其中包含一堆带有单选按钮答案的问题.当您选择答案时,Javascript 可能会启用/禁用页面上的某些问题.

I am testing a page with a bunch of questions that have radio button answers. As you select answers, Javascript may enable/disable some of the questions on the page.

问题似乎是 Selenium '点击太快'并且没有等待 Javascript 完成.我尝试以两种方式解决这个问题 - 显式等待解决了这个问题.具体来说,这有效,并解决了我的问题:

The problem seems to be that Selenium is 'clicking too fast' and not waiting for the Javascript to finish. I have tried solving this problem in two ways - explicit waits solved the problem. Specifically, this works, and solves my issue:

private static WebElement findElement(final WebDriver driver, final By locator, final int timeoutSeconds) {
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(timeoutSeconds, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);

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

但是,我更喜欢使用隐式等待而不是这个.我的网络驱动程序配置如下:

However, I would prefer to use an implicit wait instead of this. I have my web driver configured like this:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

这并不能解决问题,我得到了 NoSuchElementException.此外,我没有注意到 10 秒的暂停——它只是立即出错.我已经验证了代码中的这一行正在被调试器击中.我究竟做错了什么?为什么implicitlyWait 不等待元素出现,但FluentWait 会?

This does not solve the problem and I get a NoSuchElementException. Additionally, I do not notice a 10 second pause - it just errors out immediately. I have verified this line in the code is being hit with a debugger. What am I doing wrong? Why does implicitlyWait not wait for the element to appear, but FluentWait does?

注意:正如我提到的,我已经有一个解决方法,我真的只是想知道为什么隐式等待不能解决我的问题.谢谢.

Note: As I mentioned I already have a work around, I really just want to know why Implicit wait isn't solving my issue. Thanks.

推荐答案

记住几个场景是有区别的:

Remember that there is a difference between several scenarios:

  • DOM 中根本不存在的元素.
  • 存在于 DOM 中但不可见的元素.
  • 存在于 DOM 中但未启用的元素.(即可点击)

我的猜测是,如果某些页面使用 javascript 显示,则这些元素已经存在于浏览器 DOM 中,但不可见.隐式等待只等待一个元素出现在 DOM 中,因此它会立即返回,但是当您尝试与该元素交互时,您会收到 NoSuchElementException.您可以通过编写一个显式等待元素可见或可点击的辅助方法来检验这个假设.

My guess is that if some of the page is being displayed with javascript, the elements are already present in the browser DOM, but are not visible. The implicit wait only waits for an element to appear in the DOM, so it returns immediately, but when you try to interact with the element you get a NoSuchElementException. You could test this hypothesis by writing a helper method that explicits waits for an element to be be visible or clickable.

一些例子(Java):

Some examples (in Java):

public WebElement getWhenVisible(By locator, int timeout) {
    WebElement element = null;
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    element = wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    return element;
}

public void clickWhenReady(By locator, int timeout) {
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(locator));
    element.click();
}

这篇关于Selenium WebDriver:流畅的等待按预期工作,但隐式等待没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆