Wait until page is loaded with Selenium WebDriver for Python

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

    Find below 3 methods:

    readyState

    Checking page readyState (not reliable):

    def page_has_loaded(self):
        self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
        page_state = self.driver.execute_script('return document.readyState;')
        return page_state == 'complete'
    

    The wait_for helper function is good, but unfortunately click_through_to_new_page is open to the race condition where we manage to execute the script in the old page, before the browser has started processing the click, and page_has_loaded just returns true straight away.

    id

    Comparing new page ids with the old one:

    def page_has_loaded_id(self):
        self.log.info("Checking if {} page is loaded.".format(self.driver.current_url))
        try:
            new_page = browser.find_element_by_tag_name('html')
            return new_page.id != old_page.id
        except NoSuchElementException:
            return False
    

    It's possible that comparing ids is not as effective as waiting for stale reference exceptions.

    staleness_of

    Using staleness_of method:

    @contextlib.contextmanager
    def wait_for_page_load(self, timeout=10):
        self.log.debug("Waiting for page to load at {}.".format(self.driver.current_url))
        old_page = self.find_element_by_tag_name('html')
        yield
        WebDriverWait(self, timeout).until(staleness_of(old_page))
    

    For more details, check Harry's blog.

提交回复
热议问题