TesterInSA
TesterInSA

Reputation: 21

Selenium WeDriver FluentWait

I'm having a problem in my script. I'm using Selenium WebDriver to drive a webpage, but i'm getting ElementNotFound exceptions quite regularly. The page takes a second or two to load.

My code is the following:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(10, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);        
    try
    {
        WebElement username =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='gwt-TextBox']")));
        username.sendKeys(usernameParm);
    }
    catch (Exception e) {

        e.printStackTrace();
    }

The exception still gets thrown after a second or so. Then if i test it by running the following:

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(10, TimeUnit.SECONDS)
            .pollingEvery(1, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class);        
    try
    {
        WebElement username =  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@class='gwt-TextBox1']")));
        username.sendKeys(usernameParm);
    }
    catch (Exception e) {

        e.printStackTrace();
    }

Knowing that TexBox1 doesn't exist, then it throws the same exception. It doesn't appear to be waiting. In the second instance i would expect it to time out, and not throw ElementNotFoundException.

My implementation is probably wrong.

Upvotes: 2

Views: 483

Answers (2)

TesterInSA
TesterInSA

Reputation: 21

Yes, i'm answering my own question. Figured out what the problem was.

I had imported java.lang.util.NoSuchElementException

I had told FluentWait to ignore NoSuchElementException

What was actually being thrown was org.openqa.selenium.NoSuchElementException

Once i changed it, it seems to work just fine.

Before i figured this out, i also implemented this:

for (int i = 0; i< 10; i++){
        if (B_Username){
            break;
        } else {
            try {
                WebElement username = driver.findElement(By.xpath("//*[@class='gwt-TextBox']"));
                B_Username = true;
                username.sendKeys(usernameParm);
            } catch (NoSuchElementException e) {
                System.out.println("Caught exception while locating the username textbox");
                System.out.println(i);
                try { 
                    Thread.sleep(500);
                } catch (InterruptedException ee) {
                    System.out.println("Somthing happened while the thread was sleeping");
                }   
            }
        }
    }

which worked fine as well. Maybe this will help someone else another time. Thanks to everyone who answered.

Upvotes: 0

imalittletester
imalittletester

Reputation: 21

Checkout my post on this topic: https://iamalittletester.wordpress.com/2016/05/11/selenium-how-to-wait-for-an-element-to-be-displayed-not-displayed/. There are code snippets there. Basically my suggestion is not to use FluentWait, but instead:

WebDriverWait wait = new WebDriverWait(driver, TIMEOUT);
    ExpectedCondition elementIsDisplayed = new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver arg0) {
      try {
         webElement.isDisplayed();
         return true;
      }
      catch (NoSuchElementException e ) {
        return false;
      }
      catch (StaleElementReferenceException f) {
        return false;
      }
        } 
    };
    wait.until(elementIsDisplayed);

Define TIMEOUT with whatever timeout value seems ok for you (i believe you said 10 seconds in your initial issue).

Upvotes: 2

Related Questions