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

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 presence_of_element_located method.

For instance, we write

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Firefox()
driver.get('url')
timeout = 5
try:
    element_present = EC.presence_of_element_located((By.ID, 'element_id'))
    WebDriverWait(driver, timeout).until(element_present)
except TimeoutException:
    print("Timed out waiting for page to load")

to open the page with the given URL with get.

And then we wait for the element with the given ID by calling presence_of_element_located with (By.ID, 'element_id').

Then we have

WebDriverWait(driver, timeout).until(element_present)

to wait until the element is present.

Conclusion

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