Wait Until Page Is Loaded With Selenium WebDriver For Python
Last Updated :
06 Oct, 2024
Selenium is an automation tool or a web framework used mainly in testing web applications across various browsers. Apart from testing web applications, we can also perform various tasks with selenium. With the help of selenium, we can also perform various web related tasks such as web scraping, web element interaction ( this includes form filling, clicking buttons and many more such things) etc. It also allows user to write scripts in various languages such as Python, Java, C# etc.
In this article, we are going to explore how we can perform "Wait until page is loaded with selenium WebDriver" task. We will explore many ways to achieve this task with clear and concise examples along with their respective explanations.
Understanding Page Load in Selenium
Page loading in selenium generally refers to the process where Selenium WebDriver waits for the web page to be fully loaded before we can perform any actions on it. If the page is not fully loaded, we might not perform our desired tasks as some of the web elements are not fully loaded. This can run us into errors. We can overcome this issue with the help of with the help of two methods, i.e., WebDriverWait method and page_load_strategy options. We will briefly discuss both the methods.
Using WebDriverWait
In Selenium, we can use WebDriverWait to wait for a specific condition to be true. If , there is any error or condition cannot be true we can simply throw an error or display the appropriate message to the user. This can generally used, when some elements might end up taking more time to load than expected. In such cases, this method is proved to be a lifesaver. We can always use this method, when we are not sure about how much time a particular element will end up taking to load on which we have decided to perform actions.
Example:
In this code, we have utilized WebDriverWait function to perform our task. We have created a user-defined function pageLoad where we used try and except block to handle any unpredictable errors. We have set the wait time seconds, if the page fails to load then control flows to the except block. We have also used By class of selenium to locate for a specific web element. If we found the element, then we will perform our actions and close the browser. Otherwise, we will throw the use appropriate message.
Python
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
def pageLoad(url):
dr = webdriver.Chrome()
dr.get(pageUrl)
wait = 5
try:
#Checking our desired element is loaded or not
element = WebDriverWait(dr, wait).until(
ec.presence_of_element_located((By.CSS_SELECTOR,
'.HomePageTopicCard_homePageTopicCard__eePhS.row2')))
print("You can procced to scrape the data.....\n")
#We are now performing actions with our desired element
c=1
print("Our Web Dev Courses :-")
for i in dr.find_elements(By.CSS_SELECTOR,
'.HomePageTopicCard_homePageTopicCard__eePhS.row2 '):
print(str(c)+". ",i.text)
c += 1
except TimeoutException:
#If our desired element is not found
print("An ERROR Occured!!!!")
dr.quit()
if __name__ == "__main__":
#our url in which we want to visit and perform actions
pageUrl = "https://www.geeksforgeeks.org"
pageLoad(pageUrl)
Output:
Using WebDriverWaitUsing page_load_strategy
In selenium, page_load_strategy is used define how the selenium WebDriver will handles the loading of a web pages. We can choose the specific behavior of page load i.e. NORMAL, EAGER or NONE. By default, it is NORMAL but we can change this to EAGER of NONE for faster execution
- NORMAL : In this, driver waits for the entire page to load with all its web elements before performing any actions.
- EAGER : In this, driver waits for only for the initial HTML document to be fully loaded but, it do not wait for any other web elements like images, subframes to load.
- NONE : In this, driver do not wait at all. We can start performing any actions after the get() method is called.
Example:
In this code, we have used page_load_strategy option to achieve our task. In this, we have defined our url and passed it to our user defined function pageLoad(). In this, we have set the page the page strategy as 'normal', you can set it to eager or none according to your use. As we have used normal, our driver waits for the page to be completely loaded before performing any actions on it.
Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
def pageLoad(url):
options = Options()
# we initially set the page load stategy to normal
# which means it wait for the complete web pages to load.
options.page_load_strategy = 'normal'
dr = webdriver.Chrome(options=options)
dr.get(url)
#we are performing action here
print(dr.title)
#closing the browser
dr.quit()
if __name__ == "__main__":
#Our url is defined here
pageUrl = "https://www.geeksforgeeks.org"
pageLoad(pageUrl)
Output:
Using page_load_strategyHandling Asynchronous Content
In selenium, handling asynchronous content means waiting for the dynamic elements of an web-page to load. We can handle this asynchronous content using WebDriverWait function along with ExpectedConditions (elementToBeClickable, presenceOfElementLocated, or visibilityOf), such as waiting for elements to be clickable or visible or waiting for a class element to loaded fully. Through this we can ensure that, we will not run into any unexpected errors. This will help us to ensure that our automation scripts don't fail due to trying to interact with elements before asynchronous content has finished loading.
Conclusion
Selenium is a great automation tool we used mainly for testing web applications. But this is not the only use of selenium , we can also perform tasks such as web scrapping or web element interactions ( such as clicking buttons or filling forms). Page load is a process where selenium WebDriver waits for the web page to be fully loaded before performing any actions. We have covered two methods here to achieve our task, they are WebDriverWait method and page_load_strategy options. We have discussed all the mentioned points and tried to elaborate them accurately, concisely, and clearly.
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read