Non blocking wait in selenium using Python
Last Updated :
11 Nov, 2021
Prerequisite : Browser Automation Using Selenium
When we want to do web automation, we require to wait for some javascript elements to load before we perform some action. For that matter, generally people use
Python3
which is a blocking call.
By blocking call I mean, it waits or rather makes the program sleep for mentioned seconds no matter what happens. This isn't a good idea as it increases the latency by making the program effectively slower.
The possible solution to this is to wait until a element appears and not wait for more than that.
Pre-requisites: Python installed and Selenium installed as package along with the web driver (.exe file)
For Python Web Automation with Selenium, this can be achieved as follows:
Let's say you want to login on GeeksForGeeks through web automation and fill the login credentials as soon as username and password elements are visible on the web page and not wait until the whole page is loaded.
Step1:
You configure the webdriver as follows:
Python3
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument("disable-infobars")
chrome = webdriver.Chrome(the_path_of_webdriver_which_is_an_exe,
chrome_options = options, service_args =['--ignore-ssl-errors = true'])
login_uri = 'https://auth.geeksforgeeks.org/'
username = 'something'
password = 'anything'
username_xpath = '//*[@id ="luser"]'
password_xpath = '//*[@id ="password"]'
sign_in_xpath = '//*[@id ="Login"]/button'
chrome.get(login_uri)
Here I've used chrome web driver which would start maximized (full window) with no infobars i.e it won't say that chrome is being controlled by automation code and load the sign page of GFG without any hassle.
Do Note that in order to find the xpath of these elements you need to get into the developer mode and inspect these elements.
Step 2:
Python3
# return True if element is visible within 30 seconds, otherwise False
def is_visible(locator, timeout = 30):
try:
ui.WebDriverWait(chrome, timeout).until(EC.visibility_of_element_located((By.XPATH, locator)))
return True
except TimeoutException:
return False
The above function is_visible is facilitator of the non blocking call we intend to discuss here.
Explanation:
1) locator - the xpath of the element
2) timeout - until when to wait for the element to appear (because we don't want to wait forever)
3) chrome - the webdriver object we initialized earlier
4) It utilizes the inbuild utility of ui to make the web driver wait until the element is visible (identified by xpath)
5) if it does appear within the timeout it returns True else False
Step 3:
This is how we utilize the function:
Python3
if not is_visible(username_xpath): raise RuntimeError("Something went wrong with the username field :(")
username_field = chrome.find_element_by_xpath(username_xpath)
username_field.send_keys(username)
if not is_visible(password_xpath): raise RuntimeError("Something went wrong with the password field :(")
password_field = chrome.find_element_by_xpath(password_xpath)
password_field.send_keys(password)
if not is_visible(sign_in_xpath): raise RuntimeError("Something went wrong with the sign in field :(")
sign_in_btn = chrome.find_element_by_xpath(sign_in_xpath)
sign_in_btn.click()
Here we call the is_visible function and pass the xpath of username, password and sign_in button respectively and wait for the element to appear within timeout (here 30s). If not visible then we raise an RuntimeError with appropriate message.
If it appears anytime earlier than 30s it proceeds and find the element by xpath (as now it is visible on the webpage so this call wouldn't throw exception error.
We then send the data and click on sign in and you can enjoy learning on GFG without any blocking call :P
Similar Reads
Explicit waits in Selenium Python Selenium Python is one of the great tools for testing automation. These days most web apps are using AJAX techniques. When the browser loads a page, the elements within that page may load at different time intervals. Table of Content What is Explicit Waits? How to create an Explicit wait in Selenium
4 min read
Implicit Waits in Selenium Python Selenium Python is one of the great tools for testing automation. These days most of the web apps are using AJAX techniques. When a page is loaded by the browser, the elements within that page may load at different time intervals. This makes locating elements difficult: if an element is not yet pres
2 min read
Locating single elements in Selenium Python Locators Strategies in Selenium Python are methods that are used to locate elements from the page and perform an operation on the same. Seleniumâs Python Module is built to perform automated testing with Python. Selenium Python bindings provide a simple API to write functional/acceptance tests using
5 min read
Upload File With Selenium in Python Uploading files using Selenium in Python allows developers to automate the process of interacting with file input elements on web pages. Leveraging the Selenium WebDriver, this tutorial guides users through the steps of locating file input elements, providing the file path for upload, and handling t
2 min read
How to access popup login window in selenium using Python Many websites use sign-in using social media to make the login process easy for users. In most cases, if the button is clicked then a new popup window is opened where the user has to enter their user credentials. Manually one can switch windows in a browser and enter the required credentials to log
3 min read
Writing Tests using Selenium Python Selenium's Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This art
2 min read
Action Chains in Selenium Python Seleniumâs Python Module is built to perform automated testing with Python. ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, keypress, and context menu interactions. This is useful for doing more complex actions like hovering over and drag and
4 min read
Navigating links using get method - Selenium Python Selenium's Python module allows you to automate web testing using Python. The Selenium Python bindings provide a straightforward API to write functional and acceptance tests with Selenium WebDriver. Through this API, you can easily access all WebDriver features in a user-friendly way. This article e
2 min read
Get all text of the page using Selenium in Python As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A
3 min read
Check High School Result using Selenium in Python We are going to study check high school result status pass or fail by using selenium. This is very useful for schools because when they check how many student pass-fail and what is the name of a fail student. If the student amount is 10 and less than 10 then check easily by manual when if the number
3 min read