Wait until page is loaded with Selenium WebDriver for Python.

SeleniumAutomation TestingTesting ToolsPython

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.

Example

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")
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
// presence_of_element_located expected condition wait for 8 seconds
try:
   w = WebDriverWait(driver, 8)
   w.until(expected_conditions.presence_of_element_located((By.TA
   G_NAME,"h1")))
   print("Page load happened")
exception TimeException:
   print("Timeout happened no page load")
driver.close()

Output

raja
Published on 26-Oct-2020 06:19:00
Advertisements