Wait until page is loaded with Selenium WebDriver for Python.

We can wait until the page is loaded with Selenium webdriver. There is a synchronization concept in Selenium which describes implicit and explicit wait. To wait until the page is loaded we shall use the explicit wait concept.

The explicit wait is designed such that it is dependent on the expected condition for a particular behavior of an element. For waiting until the page is loaded we shall use the expected condition presence_of_element_loaded for a particular element. Once the wait time is elapsed, the timeout error shall be thrown.

To implement explicit wait conditions, we have to take help of the WebDriverWait and ExpectedCondition class. Let us check the presence of the element below on the page and verify if the page is loaded.


Code Implementation

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
driver = webdriver.Chrome (executable_path="C:\chromedriver.exe")
// presence_of_element_located expected condition wait for 8 seconds
   w = WebDriverWait(driver, 8)
   print("Page load happened")
exception TimeException:
   print("Timeout happened no page load")


Updated on: 26-Oct-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started