Implicit and Explicit Wait in Selenium



Implicit and Explicit wait in Selenium



We often get openQA.Selenium.NoSuchElementException when we are trying to perform an action on html elements before they are even loaded.
To avoid this exception selenium provides two type of wait methods:-
1.Implicit Wait
2.Explicit wait


Implicit wait
An implicit wait is used to tell the WebDriver to wait for a certain amount of time when trying to find an element or elements if they are not available.
The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.


Example:To wait for implicitly for 5 seconds before we click on "Sign in" button in "www.google.com"

IWebDriver chrome = new ChromeDriver(); //Opens chrome browser                    
chrome.Navigate().GoToUrl("https://google.com");//Opens www.google.com


//waits for 5 seconds until "Sign in" button is available          

chrome.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

 //Finds "Sign in" button
 IWebElement sign_btn = chrome.FindElement(By.Id("gb_70"));
   

 //Clicks on "Sign in" Button       
 sign_btn.Click();


Explicit wait
An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code.
The worst case of this is thread.sleep(), which sets the condition to an exact time period to wait.


Syntax:Thread.sleep(int milli_sec);


In our Next tutorial let us learn about select class in Selenium


No comments:

Post a Comment