【问题标题】:scroll down with selenium用硒向下滚动
【发布时间】:2015-04-21 17:19:35
【问题描述】:

我正在使用 python selenium 包,我正在尝试将页面滚动到底部。虽然,我应用 selenium 的站点是动态的(动态加载),这意味着它并没有真正滚动到底部页面。

阅读后,我在 selenium 文档中找到了该解决方案:

for i in range(25):
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(0.5)

这对我来说还不够好,因为有时页面需要更多的迭代,而有时需要更少的迭代才能到达页面底部。

是否有一个 get 函数,所以我可以使用 if 语句,例如:

for i in range(25):
    if driver.current_position == driver.document_height:
        break
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(0.5)

所以,当我到达文档的末尾时,我不会等待并打破循环。

或者如果我可以检查我跳跃了多少像素,如果它是零,我将打破循环

【问题讨论】:

    标签: python python-2.7 selenium selenium-webdriver


    【解决方案1】:

    最好使用while 循环,并在您到达页面底部后跳出循环,而不是在for 中设置一定数量的迭代。关于一种方式的讨论很好要做到这一点,在这里:Scrolling down a page with Selenium Webdriver

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-04
      相关资源
      最近更新 更多