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

前端 未结 18 1951
孤街浪徒
孤街浪徒 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: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);
    

提交回复
热议问题