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') driver.get("https://codoid.com") driver.implicitly_wait(10)
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 selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as cond from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException driver = webdriver.Chrome(executable_path='chromedriver.exe') wait = WebDriverWait(driver, timeout=10) try: driver.get("http://google.com") wait.until(cond.visibility_of_element_located((By.NAME,"txt"))) except TimeoutException as e: print("Timed Out") finally: driver.quit()
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.
wait.until(cond.alert_is_present)
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")) elemnt.send_keys("Codoid")
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.
Comments(0)