1
votes

I have a Selenium WebDriver test that is failing after dismissing a modal window then waiting for an element. Specifically, it fails looking for an element in a FluentWait call. I've tried various different ways of setting up the wait, but none are working. Here is what I think should work:

The test code that is failing:

driver.findElement(By.className("dismiss")).click(); // this dismisses a modal window
driver.switchTo().defaultContent();

FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
            .withTimeout(30, TimeUnit.SECONDS)
            .pollingEvery(500, TimeUnit.MILLISECONDS)
            .ignoring(NoSuchElementException.class);

fluentWait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.className("next"))));

driver.findElement(By.className("next")).click();

Here the failure happens looking for the element in the wait call:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"next"} Command duration or timeout: 1.33 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32' System info: host: '7rzrgv1.bsolver.local', ip: '10.0.3.103', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-431.el6.x86_64', java.version: '1.7.0_60' Session ID: 56c5a752-68fd-4cdb-ae57-2fcefc930f6f Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true, cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox, handlesAlerts=true, browserConnectionEnabled=true, webStorageEnabled=true, nativeEvents=false, rotatable=false, locationContextEnabled=true, applicationCacheEnabled=true, takesScreenshot=true, version=24.6.0}]

If I replace the fluentWait.until call with a Thread.sleep(5000), the click() works every time.

An interesting note... this only occurs when running the test on a headless machine running Firefox on top of Xvfb. It doesn't occur when an actual GUI is present.

So, a couple of questions:

Why is FluentWait not ignoring the NoSuchElementException ?

Does anyone have thoughts on a more elegant way to deal with this other than an implicit wait?

1
Can you show your imports? I guess you are using NoSuchElementException from some other package. - Ajinkya
Just checked my imports, I'm importing org.openqa.selenium.NoSuchElementException - Axl

1 Answers

6
votes

I had the same problem. @Karna and @Axl helped me figure it out. I was using import java.util.NoSuchElementException; instead of import org.openqa.selenium.NoSuchElementException;.