Categories
Python Answers

How to wait until page is loaded with Selenium WebDriver for Python?

Spread the love

Sometimes, we want to wait until page is loaded with Selenium WebDriver for Python In this article, we’ll look at how to wait until page is loaded with Selenium WebDriver for Python

How to wait until page is loaded with Selenium WebDriver for Python?

To wait until page is loaded with Selenium WebDriver for Python, we can use the until method.

For instance, we write

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

browser = webdriver.Firefox()
browser.get("url")
delay = 3
try:
    myElem = WebDriverWait(browser, delay).until(EC.presence_of_element_located((By.ID, 'IdOfMyElement')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

to create a WebDriverWait instance with the browser and a 3 seconds delay.

Then we call until with the element to wait for, which we get with

EC.presence_of_element_located((By.ID, 'IdOfMyElement'))

Conclusion

To wait until page is loaded with Selenium WebDriver for Python, we can use the until method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *