Selenium Python – Check if page is loaded
In this tutorial, you will learn how to check if a page is loaded using Selenium in Python.
To check if a page is loaded in Selenium Python, we can wait for a specific (known) element in the webpage to be present.
In Selenium, we can implement this solution using WebDriverWait class and expected_conditions module.
1. Define Expected Condition
Firstly, define the expected condition. Call
presence_of_element_located() function of the expected_conditions module and pass the By strategy and web element locator as arguments.
For example, if we can conclude that the page is loaded fully by the presence of an element with
id="myDiv" in the webpage, then the expected condition can be defined as shown in the following code.
element_present = exprected_conditions.presence_of_element_located((By.XPATH, "//[@id='myDiv']"))
2. Wait until timeout for expected condition to satisfy
As we have already defined the expected condition, now we have to wait for that condition to satisfy in a given maximum amount of time, say timeout.
For example, if we have to wait for a maximum time of timeout, for the expected condition to fulfil, the code is as shown in the following.
If the expected condition is fulfilled, the above statement is marked complete and continues with the following statements.
But, if the expected condition is not fulfilled, the above statement raises TimeoutException.
Consider the following HTML file.
<html> <body> <h2>Hello User!</h2> <div id="parent"> <div id="child1">This is child 1.</div> <div id="child2">This is child 2.</div> <div id="child3">This is child 3.</div> </div> </body> </html>
In the following program, let us make an assumption that the presence of element with
id="child3" is an expected condition for the page to have been loaded successfully. Using this expected condition, let us check if the page is loaded successfully.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Setup chrome driver service = ChromeService(executable_path=ChromeDriverManager().install()) driver = webdriver.Chrome(service=service) # Navigate to the url driver.get('http://127.0.0.1:5500/index.html') # Set the maximum amount of time to wait for the page to load (in seconds) timeout = 10 # Wait for the presence of a specific element on the page try: element_present = EC.presence_of_element_located((By.XPATH, "//*[@id='child3']")) WebDriverWait(driver, timeout).until(element_present) print("Page loaded successfully!") except TimeoutException: print("Timed out waiting for page to load.") # Close the driver driver.quit()
Page loaded successfully!
In this Python Selenium tutorial, we have given instructions on how to check if a page is loaded, with example program.