Table of content

Waits in Selenium Webdriver

What is wait ? why we need it ?

It is the process of matching the speed of action and reaction, selenium does the action and web application shows the reaction.

Example: Click Login button gmail login (selenium does this action) web application navigates to the inbox(reaction)


Click on Compose button in Gmail

						public class Gmail 
						{
							public static void main(String[] args)
							{
								//open firefox browser
								WebDriver driver=new FirefoxDriver();
								//goto gmail.com
								driver.get("https://www.gmail.com");
								//enter text in username
								driver.findElement(By.xpath("//*[@id='email' type='email'   ]")).sendKeys("username");
								//enter text in password
								driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
								//click on the signin button
								driver.findElement(By.xpath("//*[@id='signIn']")).click();
								//click on the compose button
								driver.findElement(By.cssSelector("div[class='T-I J-J5-Ji T-I-KE L3']")).click();	
							}
						}
					

If you run the above program you might face "NoSuchElementException" Exception because selenium starts searching for the compose button immediately after clicking on the SignIn button it waits for few milli-seconds but normally gmail takes 3 to 4 second to open so selenium will not able to find it.

In most of the cases the selenium does the action in micro or milli-seconds but the application takes time in seconds.

At the same time selenium will not wait till it loads or becomes visible at the time selenium will not able to find the element within the java processing time so selenium throws “NoSuchElementException᾿.

We cannot alter the speed of the application, so we must slow down selenium.

Type of Waits in selenium webdriver

There are several ways to synchronize the selenium with application

  • Sleep
  • pageLoadTimeout
  • setScriptTimeout
  • ImplicitlyWait
  • ExplicitWait or WebdriverWait
  • FluentWait

Sleep() in Selenium webdriver

Sleep() is a method present in thread class, most of the java people might be familiar with this. It makes selenium to sleep for the specified time, we can specify the time

If you mention Thread.sleep(20); then selenium waits for 20 milli-seconds sleep method takes argument in milli-seconds as the input. If you want make the selenium to sleep for the 20 seconds means we need to mention like this Thread.sleep(20000);

Sleep() method will not worry about whether the element is present or not, it just sleeps till mentioned time. This is also called as static wait or blind wait, dead wait.


						Syntax : Thread.sleep(Long milli-seconds);
						


Complete program for Sleep()


						public class A 
						{
							public static void main(String[] args) throws InterruptedException 
							{
								//open firefox
								WebDriver driver=new FirefoxDriver();
								//open google.com
								driver.get("https://oogle.com");
								//wait for 5 seconds
								Thread.sleep(5000);
								//send text to the search bar
								driver.findElement(By.name("search bar")).sendKeys("selenium-webdriver");
								//click on the search button
								driver.findElement(By.id("q")).click();		
							}
						}
					

Create Custom By class Locator in Selenium

Code Example 2 for Sleep()

						public class GmailLogin 
						{
							public static void main(String[] args) throws InterruptedException							{
								//open the firefox
								WebDriver driver=new FirefoxDriver();
								
								//sleep for 5 seconds
								Thread.sleep(5000);
								
								//open gmail.com
								driver.get("https://www.gmail.com");
								
								//sleep for 5 seconds
								Thread.sleep(5000);
								
								//enter the user name in the Email field
								driver.findElement(By.id("Email")).sendKeys("username");
								
								//sleep for 5 seconds
								Thread.sleep(5000);
								
								//enter the password in the password field
								driver.findElement(By.id("Passwd")).sendKeys("password");
								
								//sleep for 5 seconds
								Thread.sleep(5000);
								
								//sends the enter to the password field
								//sendKeys always append the text present in the element
								//it never erases the text in the element
								driver.findElement(By.id("Passwd")).click();		
							}
						}
					

Consider the above program in which we are using sleep() method every time it waits for 10 seconds.

Let us consider practical application in that we are having more the 8 pages and 50 operations if we use Thread.sleep(10000) for 50 times then it is 500 seconds around 10 minutes, so the dead time is more but no one prefer to wait .

How to handle browser windows using selenium webdriver

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group