Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in DevOps and Agile by (19.7k points)

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.

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);

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.

1 Answer

0 votes
by (62.9k points)

The basic idea is in the following:

 

Explicit wait

WebDriverWait.until(condition-that-finds-the-element);

Implicit wait

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

In other words, explicit is associated with some condition to be held, whereas implicit with some time to wait for something. see this link

 

To make work fluentWait properly try this:

public WebElement fluentWait(final By locator){

    Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

        .withTimeout(Duration.ofSeconds(30))

        .pollingEvery(Duration.ofMillis(100))

        .ignoring(NoSuchElementException.class);

 

    WebElement foo = wait.until(

        new Function<WebDriver, WebElement>() {

            public WebElement apply(WebDriver driver) {

                return driver.findElement(locator);

            }

        }

    );

    return foo;

};

Hope this helps!

Browse Categories

...