Here are details related to using waits with Selenium WebDriver in Java. According to the Selenium documentation, at the moment, there are two different type of waits: explicit and implicit.

After you understand how to locate elements with Selenium WebDriver, you have to focus on waiting for elements to appear, be visible, be clickable.

Waiting is an automated step, elapse a certain amount of time, before execution can continue. Choose to use Explicit Waits or Implicit Waits. As a side note, you should not mix implicit and explicit waits.

Implicit Waits in Selenium WebDriver

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

The implicit wait is set for the life of the WebDriver object instance.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

//use a URL that delays loading elements
driver.get("http://www.example.com/");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));

You can set the implicit wait when you first instantiate the WebDriver:

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

This specifies the amount of time the driver should wait when searching for an element if it is not immediately present.

Explicit Waits in Selenium WebDriver

An explicit wait represents defined code for certain conditions or behaviours to occur/happen before continued to the next step.

Not a good practice is to add an exact time wait which usually is done by the Thread.sleep() method.

Or you can create a method in order to pass seconds for the explicit wait, but again this is not a best practice:

public void waitSeconds(int seconds){
	try {
		Thread.sleep(seconds * 1000);
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

Selenium WebDriver provides some out of the shelf methods that will help you write code that will wait only as long as required.

WebDriverWait in combination with ExpectedCondition is better than what an ExplicitWait can be achieved:

WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));

This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 10 seconds.

Or you can use a generic method in order to wait for elements to be present or visible:

public void waitForElement(int seconds, String waitConditionLocator){
	WebDriverWait wait = new WebDriverWait(driver, seconds);
	wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(waitConditionLocator)));
}

or

public static boolean isElementPresent(By by, WebDriver driver) {
	boolean present;
	try{
		driver.findElement(by);
		present = true;
	}catch (NoSuchElementException e){
		present = false;
	}
	return present;
}

or

there’s an alternative way by checking the count using FindElements which won’t throw an exception:

if( driver.FindElements(By.CssSelector(exportCssSelector)).Count‌() > 0) {//element exists}

WebDriverWait by default calls the ExpectedCondition every 0.5 seconds until it returns successfully. A successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.

Expected Conditions

 

There are some common conditions that are frequently encountered when automating web browsers. Listed below are a few examples for the usage of such conditions. The Java, C#, and Python bindings include convenience methods so you don’t have to code an ExpectedCondition class yourself or create your own utility package for them.
Element is Clickable – it is Displayed and Enabled.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("idOfElementToBeClicked")));

The ExpectedConditions package (Java) (Python) (.NET) contains a set of predefined conditions to use with WebDriverWait.


 

LoadFocus.com is a cloud testing platform, use Automated Website Testing Service to build functional tests and automate regression manual tests of your website, more details.