Wait Until Page Is Loaded With Selenium Webdriver For Python

Wait until page is loaded with Selenium WebDriver for Python

The webdriver will wait for a page to load by default via .get() method.

As you may be looking for some specific element as @user227215 said, you should use WebDriverWait to wait for an element located in your page:

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 # seconds
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!"

I have used it for checking alerts. You can use any other type methods to find the locator.

EDIT 1:

I should mention that the webdriver will wait for a page to load by default. It does not wait for loading inside frames or for ajax requests. It means when you use .get('url'), your browser will wait until the page is completely loaded and then go to the next command in the code. But when you are posting an ajax request, webdriver does not wait and it's your responsibility to wait an appropriate amount of time for the page or a part of page to load; so there is a module named expected_conditions.

Wait until the page loaded Selenium Python

This should help u:

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

login_btn = WebDriverWait(driver, 500).until(EC.presence_of_element_located((By.XPATH, 'xpath of login button'))) #Waits until the login button is visible.

login_btn.click()

If u don't wanna find the button by its xpath, then u can change By.XPATH to By.ID or By.CLASS_NAME.

Edit:

Here is the complete code:

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.webdriver.chrome.options import Options

chrome_options = Options()

chrome_options.add_argument("--start-maximized")

driver = webdriver.Chrome('chromedriver.exe',options = chrome_options)

driver.get('https://www.nike.com/es/launch/t/womens-air-jordan-6-tech-chrome')

login_btn = WebDriverWait(driver, 500).until(EC.presence_of_element_located((By.XPATH, '//*[@id="root"]/div/div/div[1]/div/header/div[1]/section/div/ul/li[1]/button'))) #Waits until the login button is visible.

login_btn.click()

Output Screenshot:

Sample Image

Make Selenium wait until text is available

To locate a visible element instead of presence_of_element_located() you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategy:

driver.get(url)
try:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "/html/body/app-root/app-wrapper/div/div[2]/app-etp/div[2]/div[3]/div[2]/div/div[2]/app-widget-quote-box/div/div/table/tbody/tr[3]/td[1]"))).text)
finally:
pass

Wait until element is fully loaded using selenium - python

Try this:

root_elm = driver.find_elements_by_xpath(xpath)
important_elm = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.CLASSNAME, "market-title-v3"))


Related Topics



Leave a reply



Submit