Wait until page is loaded with Selenium WebDriver for Python

后端 未结 12 985
借酒劲吻你
借酒劲吻你 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:53

    You can do that very simple by this function:

    def page_is_loading(driver):
        while True:
            x = driver.execute_script("return document.readyState")
            if x == "complete":
                return True
            else:
                yield False
    

    and when you want do something after page loading complete,you can use:

    Driver = webdriver.Firefox(options=Options, executable_path='geckodriver.exe')
    Driver.get("https://www.google.com/")
    
    while not page_is_loading(Driver):
        continue
    
    Driver.execute_script("alert('page is loaded')")
    

提交回复
热议问题