如何在python中使用selenium webdriver滚动网页?

 heyuntao 发布于 2023-02-05 12:08

我目前正在使用selenium webdriver来解析Facebook用户朋友页面并从AJAX脚本中提取所有ID.但我需要向下滚动才能吸引所有朋友.如何在Selenium中向下滚动.我正在使用python.

10 个回答
  • 您可以使用

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

    其中Y是高度(在全高清监视器上它是1080).(感谢@lukeis)

    你也可以使用

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

    滚动到页面底部.

    如果你想scrool到一个无限加载的页面,如社交网络,Facebook等(感谢@Cong 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
    

    2023-02-05 12:08 回答
  • 与此处显示的方法相同:

    在python中你可以使用

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

    (Y是您要滚动到的垂直位置)

    2023-02-05 12:08 回答
  • from selenium.webdriver.common.keys import Keys
    html = browser.find_element_by_tag_name('html')
    html.send_keys(Keys.END)
    

    测试,它的工作原理

    2023-02-05 12:09 回答
  • 如果要向下滚动到无限页面的底部(如linkedin.com),可以使用以下代码:

    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
    

    参考:https://stackoverflow.com/a/28928684/1316860

    2023-02-05 12:09 回答
  • 这些答案对我都不起作用,至少不是向下滚动Facebook搜索结果页面有效,但经过大量测试,我发现此解决方案:

    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
    

    2023-02-05 12:10 回答
  • 这是您向下滚动网页的方式:

    driver.execute_script("window.scrollTo(0, 1000);")
    

    2023-02-05 12:11 回答
  • 与youtube一起使用时,浮动元素的滚动高度为“ 0”,因此请不要使用“ return document.body.scrollHeight”,而是 根据您的互联网尝试使用此“ return document.documentElement.scrollHeight”来调整滚动暂停时间否则它将仅运行一次,然后在此之后中断。

    SCROLL_PAUSE_TIME = 1
    
    # Get scroll height
    """last_height = driver.execute_script("return document.body.scrollHeight")
    
    this dowsnt work due to floating web elements on youtube
    """
    
    last_height = driver.execute_script("return document.documentElement.scrollHeight")
    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0,document.documentElement.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.documentElement.scrollHeight")
        if new_height == last_height:
           print("break")
           break
        last_height = new_height
    

    2023-02-05 12:11 回答
  • 我发现解决该问题的最简单方法是选择一个标签,然后发送:

    label.sendKeys(Keys.PAGE_DOWN);
    

    希望它能起作用!

    2023-02-05 12:11 回答
  • element=find_element_by_xpath("xpath of the li you are trying to access")
    
    element.location_once_scrolled_into_view
    

    当我试图访问一个看不见的'li'时,这有帮助.

    2023-02-05 12:11 回答
  • 出于我的目的,我想向下滚动更多,同时牢记窗口的位置。我的解决方案是相似的,并使用window.scrollY

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

    它将转到当前的y滚动位置+ 200

    2023-02-05 12:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有