Wait until page is loaded with Selenium WebDriver for Python

后端 未结 12 876
借酒劲吻你
借酒劲吻你 2020-11-22 00:26

I want to scrape all the data of a page implemented by a infinite scroll. The following python code works.

for i in range(100):
    driver.execute_script(\"w         


        
12条回答
  •  無奈伤痛
    2020-11-22 00:57

    How about putting WebDriverWait in While loop and catching the exceptions.

    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
    
    browser = webdriver.Firefox()
    browser.get("url")
    delay = 3 # seconds
    while True:
        try:
            WebDriverWait(browser, delay).until(EC.presence_of_element_located(browser.find_element_by_id('IdOfMyElement')))
            print "Page is ready!"
            break # it will break from the loop once the specific element will be present. 
        except TimeoutException:
            print "Loading took too much time!-Try again"
    

提交回复
热议问题