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

前端 未结 18 2078
孤街浪徒
孤街浪徒 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条回答
  •  猫巷女王i
    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

提交回复
热议问题