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

前端 未结 18 1651
孤街浪徒
孤街浪徒 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:47

    Here's an example selenium code snippet that you could use for this type of purpose. It goes to the url for youtube search results on 'Enumerate python tutorial' and scrolls down until it finds the video with the title: 'Enumerate python tutorial(2020).'

    driver.get('https://www.youtube.com/results?search_query=enumerate+python')
    target = driver.find_element_by_link_text('Enumerate python tutorial(2020).')
    target.location_once_scrolled_into_view
    
    0 讨论(0)
  • 2020-11-22 07:48
    driver.execute_script("document.getElementById('your ID Element').scrollIntoView();")
    

    it's working for my case.

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

    None of these answers worked for me, at least not for scrolling down a facebook search result page, but I found after a lot of testing this solution:

    while driver.find_element_by_tag_name('div'):
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
        Divs=driver.find_element_by_tag_name('div').text
        if 'End of Results' in Divs:
            print 'end'
            break
        else:
            continue
    
    0 讨论(0)
  • 2020-11-22 07:49

    This code scrolls to the bottom but doesn't require that you wait each time. It'll continually scroll, and then stop at the bottom (or timeout)

    from selenium import webdriver
    import time
    
    driver = webdriver.Chrome(executable_path='chromedriver.exe')
    driver.get('https://example.com')
    
    pre_scroll_height = driver.execute_script('return document.body.scrollHeight;')
    run_time, max_run_time = 0, 1
    while True:
        iteration_start = time.time()
        # Scroll webpage, the 100 allows for a more 'aggressive' scroll
        driver.execute_script('window.scrollTo(0, 100*document.body.scrollHeight);')
    
        post_scroll_height = driver.execute_script('return document.body.scrollHeight;')
    
        scrolled = post_scroll_height != pre_scroll_height
        timed_out = run_time >= max_run_time
    
        if scrolled:
            run_time = 0
            pre_scroll_height = post_scroll_height
        elif not scrolled and not timed_out:
            run_time += time.time() - iteration_start
        elif not scrolled and timed_out:
            break
    
    # closing the driver is optional 
    driver.close()
    

    This is much faster than waiting 0.5-3 seconds each time for a response, when that response could take 0.1 seconds

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

    If you want to scroll down to bottom of infinite page (like linkedin.com), you can use this code:

    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
    

    Reference: https://stackoverflow.com/a/28928684/1316860

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

    You can use send_keys to simulate an END (or PAGE_DOWN) key press (which normally scroll the page):

    from selenium.webdriver.common.keys import Keys
    html = driver.find_element_by_tag_name('html')
    html.send_keys(Keys.END)
    
    0 讨论(0)
提交回复
热议问题