Selenium WebDriver Wait

Selenium WebDriver Wait

Most of the web application are using AJAX techniques. When a page is loaded to browser, the elements within that page may load at different time intervals. This makes locating elements difficult, if the element is not present in the DOM, it will raise exceptions like ElementNotVisibleException, ElementNotPresentExceptionetc.
Using waits, we can solve this issue.
So, let us discuss about different selenium waits available in Selenium WebDriver:

1.Implicit Wait

It is used to set default wait time between two steps. For example, if you want to put wait for 30 seconds, use implicit wait like below:

1
2
3
4
5
6
7
8
9
10
11
//create WebDriver object for FireFox
WebDriver driver = new FirefoxDriver();

//Set Implicit wait for 30 second
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

//open URL
driver.get("https://www.seleniumpoint.com/");

//Perform further actions....
driver.findElement(By.id("name")).sendKeys("any value");

In the above example the implicit waits value is given as 30sec i.e, if web driver able to find the element with in the given span 30sec it immediately returns true and go to next step else it raises an exception after 30 seconds.

IMP: Implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for.

2.Explicit Wait

Explicit waits are used to pause the execution till the time a particular condition is occur or the maximum time has elapsed.Explicit waits are applied for a particular instance only.
WebDriver introduces classes like WebDriverWait and ExpectedConditions to enforce Explicit waits into the test scripts.
For example:
If you are writing a script for login to Gmail account, you will enter username , password and click on Sign In button. Then wait till the Compose button appearing on next page. This is nothing but explicit wait.

Selenium Script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package scripts;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExplicitWait {

public static void main(String[] args) throws InterruptedException {

//set Gecko driver for FireFox browser
System.setProperty("webdriver.gecko.driver", "C:\\harshal personal\\selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");

//create WebDriver object for FireFox
WebDriver driver = new FirefoxDriver();

//open URL
driver.get("https://www.gmail.com/");

//enter username
driver.findElement(By.id("Email")).sendKeys("harshaljshewale@gmail.com");

//click next
driver.findElement(By.id("next")).click();

//Explicit wait till SignIn Button visible
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("signIn"))));

//enter password
driver.findElement(By.id("Passwd")).sendKeys("YourPwd@xyzcom");

//click on Sign In button
driver.findElement(By.id("signIn")).click();

//Explicit wait till Compose Button to be clickable
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='z0']")));

driver.findElement(By.xpath("//div[@class='z0']")).click();


}

}

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×