Select Page
Automation Testing

Selenium Wait Commands using Python to Avoid False Positives

Implicit Wait applies the wait time for the entire browser session and you can change the wait time during run-time.

Selenium Wait Commands using Python to Avoid False Positives
Listen to this blog

We all know that Selenium has three wait types (Implicit, Explicit, and FluentWait). How to avoid false positives using Selenium waits? Applying wait for all FindElement steps using Implicit Wait is not a right approach. If you are automating a small website or writing some Web scraping scripts, you can go with Implicit Wait. However, if you are writing automated test scripts for an web application, then our recommendation is Explicit Wait. Moreover, Implicit Wait applies the wait time for the entire browser session and you can change the wait time during run-time, however, you can’t do it for individual FindElement method call. As an automation testing company, we use Selenium Explicit Wait to avoid false positives as much as possible. In this blog article, you are going to learn Selenium Wait Commands using Python.

Implicit Wait

As we mentioned before, use Implicit Wait for simple website or Web scraping scripts and don’t mix Implicit and Explicit Waits. When you automate a full regression test suite, it requires proper Page Load and AJAX call handling using Explicit Wait. Note: By default, Implicit Wait is disabled.

from selenium import webdriver
driver = webdriver.Chrome(executable_path='chromedriver.exe')

Visibility of Element Located

Checking the presence of an element in DOM will not suffice our need. In addition to that the scripts need to confirm whether the web element is displayed on the web page or not. Selenium Explicit Wait has visibility_of_element_located method to check whether the web element is available in DOM and Height & Width is greater than ‘0’. Most of the automation testers are aware of this method/technique. However for novice QA automation testers to write robust automation test scripts this will be a much useful tip. Your developers can hide web elements for different reasons using the below CSS attributes.

<input type="text" id="txt1" style="opacity: 0"/>
<input type="text" id="txt1" style="visibility: hidden"/>
<input type="text" id="txt1" style="display: none"/>
<input type="text" id="txt1" style="position: absolute;top: -9999px;left: -9999px;"/>
<input type="text" id="txt1" style="clip-path: polygon(0px 0px,0px 0px,0px 0px,0px 0px);"/>

Note: If any HTML tag is styled using clip-path, Selenium’s visibility check does not work. This may be an issue with Selenium. Let’s wait for the comments.

from selenium import webdriver
from import WebDriverWait
from import expected_conditions as cond
from import By
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome(executable_path='chromedriver.exe')
wait = WebDriverWait(driver, timeout=10)

except TimeoutException as e:
    print("Timed Out")

Wait for JavaScript Alert Presence

JavaScript Alert will pops out on your web browser after its invocation. One should use wait mechanism while handling JS Alert to avoid script failure. Sometimes, due to Ajax call response delay, JavaScript Alert invocation will also be delayed. However, your automation script should wait for the expected delay to handle the alert.


Selenium Explicit Wait using Lambda Expression

You can also use Python Lambda Expression to simplify the Selenium Explicit Wait commands.

elemnt = wait.until(lambda d: d.find_element_by_id("txt"))

In Conclusion

As a leading test automation services company, we at Codoid follow best practices to develop robust automation testing scripts. We hope you have learnt the nuances of Selenium Explicit Wait techniques to avoid false positives from this blog article. Contact us for your automation testing needs.


Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us