How can I scroll a web page using selenium webdriver in python?

前端 未结 18 1689
孤街浪徒
孤街浪徒 2020-11-22 07:04

I am currently using selenium webdriver to parse through facebook user friends page and extract all ids from the AJAX script. But I need to scroll down to get all the friend

18条回答
  •  太阳男子
    2020-11-22 07:35

    I was looking for a way of scrolling through a dynamic webpage, and automatically stopping once the end of the page is reached, and found this thread.

    The post by @Cuong Tran, with one main modification, was the answer that I was looking for. I thought that others might find the modification helpful (it has a pronounced effect on how the code works), hence this post.

    The modification is to move the statement that captures the last page height inside the loop (so that each check is comparing to the previous page height).

    So, the code below:

    Continuously scrolls down a dynamic webpage (.scrollTo()), only stopping when, for one iteration, the page height stays the same.

    (There is another modification, where the break statement is inside another condition (in case the page 'sticks') which can be removed).

        SCROLL_PAUSE_TIME = 0.5
    
    
        while True:
    
            # Get scroll height
            ### This is the difference. Moving this *inside* the loop
            ### means that it checks if scrollTo is still scrolling 
            last_height = driver.execute_script("return document.body.scrollHeight")
    
            # Scroll down to bottom
            driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    
            # Wait to load page
            time.sleep(SCROLL_PAUSE_TIME)
    
            # Calculate new scroll height and compare with last scroll height
            new_height = driver.execute_script("return document.body.scrollHeight")
            if new_height == last_height:
    
                # try again (can be removed)
                driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    
                # Wait to load page
                time.sleep(SCROLL_PAUSE_TIME)
    
                # Calculate new scroll height and compare with last scroll height
                new_height = driver.execute_script("return document.body.scrollHeight")
    
                # check if the page height has remained the same
                if new_height == last_height:
                    # if so, you are done
                    break
                # if not, move on to the next loop
                else:
                    last_height = new_height
                    continue
    

提交回复
热议问题