Prerequisite : Browser Automation Using Selenium
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.
You configure the webdriver as follows:
Here I’ve used chrome web driver which would start maximised (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.
The above function is_visible is facilitator of the non blocking call we intend to discuss here.
1) locator – the xpath of the element
2) timeout – until when to wait for the element to appear (beacuse we don’t want to wait forever)
3) chrome – the webdriver object we initialized earlier
4) It utilizes the inbuild utlity 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
This is how we utilize the function:
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 withing 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 😛
- Python | SMS Bomber using Selenium
- Selenium Python Tricks
- How to install Selenium in Python
- Like instagram pictures using Selenium | Python
- Why do people prefer Selenium with Python?
- Python | os.wait() method
- Python | Automate Google Search using Selenium
- How to access popup login window in selenium using Python
- Python | Automating Happy Birthday post on Facebook using Selenium
- Browser Automation Using Selenium
- Find Web Elements using Selenium WebDriver
- Software Engineering | Selenium: An Automation tool
- Differences between wait() and join() methods in Java
- Difference between Stop and Wait, GoBackN and Selective Repeat
- How to wait for a promise to finish before returning the variable of a function?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.