1

What is the best way to wait for an element to appear on a web page? I have read that we can use implicit wait and functions like webdriverwait, fluentwait etc and last but not the least thread.sleep()...which i use the most but want to stop using at all.

My scenario:

User logs in to a website...website checks the credentials and provides an offer to the user in the form of an overlay (kind of popup but not a separate window). I need to verify text on the the overlay. There is a time gap between user signing in and the overlay getting displayed. what is the best approach so that selenium waits only till the time the element is not visible. As the overlay is not a separate page but part of the main page, implicit wait does not work at all.

All suggestions are welcome...:)

vaibhav misra
  • 135
  • 6
  • 19
  • I would recommend fluent wait as it gives lot of control over the ping time and the total duration. But the implicit wait should also work unless you have specified very little time. Probably you are checking for the element presence and code is breaking at that point. – Vinay Aug 05 '13 at 16:51
  • I would recommend using webdriverwait or fluentwait. I don't like implicit waiting, and I definitely don't like sleep(), because both often cause waiting when there is no need to. There are quite a ExpectedConditions that you can wait for with wait.until() – Nathan Merrill Aug 05 '13 at 16:54

4 Answers4

1
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.id("optionsBuilderSelect_input")));

I'm a professional scraper (http://nitinsurana.com) I've written 30+ softwares using selenium and I've never faced any such issue, anyways above is a sample code.

All I can think of is that what until condition has to be checked because many a times elements are already visible, but they are not clickable and things like that. I guess you should give different options a try and I hope you'll find the one required.

coding_idiot
  • 13,526
  • 10
  • 65
  • 116
0

Always start by using a implicit wait. I think Selenium defaults to 5 seconds and so if you do a driver.findElement(), the implication is that it will wait up to 5 seconds. That should do it. If you are experiencing a scenario where the time it takes is unpredictable, then use FluentWait (with the same 5 second timeout) but also using the .ignoring method and wrap that inside a while loop . Here is the basic idea:

int tries=0;
while ( tries < 3 ) {
  //fluent wait (with .ignoring) inside here
  tries ++1;
}
djangofan
  • 28,471
  • 61
  • 196
  • 289
0
public boolean waitForElement(WebElement ele, String xpath, int seconds) throws InterruptedException{
    //returns true if the xpath appears in the webElement within the time
    //false when timed out
    int t=0;
    while(t<seconds*10){
        if(ele.findElements(By.xpath(xpath)).size()>0)
            return true;
        else{
            Thread.sleep(100);
            t++;
            continue;
        }
    }       
    System.out.println("waited for "+seconds+"seconds. But couldn't find "+xpath+ " in the element specified");
    return false;
}
cegprakash
  • 2,937
  • 33
  • 60
0

You could wait for the presence of the element to appear as follows:

new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("someId")));