Python Selenium
About Lesson

In this Python Selenium lesson we are going to talk about Selenium Implicit & Explicit Waits, first we want to learn about selenium implicit waits after that we are going to learn about selenium explicit waits. There will be situations that your test script will not find an element in the web page, because the page is loading slowly, or even that web element is not available in the web page, this could happen due to different reasons, for example the web element on the web page is not loaded by the time your test scripts tries to find it. on that time we will receive negative results or error from our test scripts, so we need to match the speed of the test script with the application’s speed by introducing delays in the test script. now there are two methods that you can use in selenium web driver we have implicit and explicit wait.

 

 

 

Python Selenium Implicit Waits

The implicit wait offers a generic way to synchronize the entire test or group of steps in WebDriver. for example we have two pages, one is on the local server and the second is on the remote server, so the load time for the page that is in the local server will be less than the page that is hosted in a server. now we want to execute test cases against each of them, we should configure the wait time accordingly and we can use implicit wait time, by default the implicit wait time is zero. 

now it will wait for maximum 10 seconds if there was any wait on the elements or loading the page. or we can say that This waits up to 10 seconds before throwing a TimeoutException unless it finds the element to return within 10 seconds. you can use the implicit wait for all elements in the page and it is available for all the elements

 

 

Run the code it will first open the website and after that it will find the web elements, there will be a maximum of 10 seconds wait for every web elements. right now you will not see much effects, and the effect will be on that time for example the page loading takes some time and you will see the effects.

 

Python Selenium - Selenium Implicit & Explicit Waits
Python Selenium – Selenium Implicit & Explicit Waits

 

Python Selenium Explicit Waits

OK we have learned about implicitly_wait(), and you can see that we have added 10 seconds implicit wait, now there may be some situations, for example we have added this 10 second wait, and this wait is for all web elements, for example maybe in this 10 seconds the page could not be loaded, or our code could not find a web element in the page during this maximum 10 second waits, on that time we will have an exception or an error, because we implicitly specified the 10 seconds wait. to solve this problem there is explicitly_wait(). 

 

Explicit wait is another wait mechanism that we can use in web driver and it provides a better control compared to implicit wait, because implicit wait is generic to all web elements in the web page, but if you have one specific web element in your web page where you want to wait for a long time, than it is good to use explicit wait, because in explicit wait we can use a set of predefined or custom conditions for the script to wait for before processing with further steps. selenium WebDriver provides the WebDriverWait and expected_conditions classes to implement an explicit wait.

 

now in this code Selenium web driver will wait for a maximum of 10 seconds for an element matching the given criteria to be found. If no element is found or clicked in that time, a TimeoutException is thrown. By default, WebDriverWait calls the ExpectedCondition every 500 milliseconds until it returns success. ExpectedCondition will return true in case of success or not null if it fails to locate an element.