Wait until page is loaded with Selenium WebDriver for Python

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

    On a side note, instead of scrolling down 100 times, you can check if there are no more modifications to the DOM (we are in the case of the bottom of the page being AJAX lazy-loaded)

    def scrollDown(driver, value):
        driver.execute_script("window.scrollBy(0,"+str(value)+")")
    
    # Scroll down the page
    def scrollDownAllTheWay(driver):
        old_page = driver.page_source
        while True:
            logging.debug("Scrolling loop")
            for i in range(2):
                scrollDown(driver, 500)
                time.sleep(2)
            new_page = driver.page_source
            if new_page != old_page:
                old_page = new_page
            else:
                break
        return True
    

提交回复
热议问题