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

前端 未结 18 1652
孤街浪徒
孤街浪徒 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
    
    0 讨论(0)
  • 2020-11-22 07:39

    This is how you scroll down the webpage:

    driver.execute_script("window.scrollTo(0, 1000);")
    
    0 讨论(0)
  • 2020-11-22 07:42

    For my purpose, I wanted to scroll down more, keeping the windows position in mind. My solution was similar and used window.scrollY

    driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
    

    which will go to the current y scroll position + 200

    0 讨论(0)
  • 2020-11-22 07:42

    The easiest way i found to solve that problem was to select a label and then send:

    label.sendKeys(Keys.PAGE_DOWN);
    

    Hope it works!

    0 讨论(0)
  • 2020-11-22 07:43

    You can use

    driver.execute_script("window.scrollTo(0, Y)") 
    

    where Y is the height (on a fullhd monitor it's 1080). (Thanks to @lukeis)

    You can also use

    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    

    to scroll to the bottom of the page.

    If you want to scroll to a page with infinite loading, like social network ones, facebook etc. (thanks to @Cuong Tran)

    SCROLL_PAUSE_TIME = 0.5
    
    # Get scroll height
    last_height = driver.execute_script("return document.body.scrollHeight")
    
    while True:
        # 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:
            break
        last_height = new_height
    

    another method (thanks to Juanse) is, select an object and

    label.sendKeys(Keys.PAGE_DOWN);
    
    0 讨论(0)
  • 2020-11-22 07:45

    The ScrollTo() function doesn't work anymore. This is what I used and it worked fine.

    driver.execute_script("document.getElementById('mydiv').scrollIntoView();")
    
    0 讨论(0)
提交回复
热议问题