如何在 Python 中使用 selenium 滚动到页面末尾? [英] How to scroll to the end of the page using selenium in Python?

查看:99
本文介绍了如何在 Python 中使用 selenium 滚动到页面末尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试滚动到页面末尾,以便我可以显示所有数据并提取它.我试图为它找到一个命令,但它在 java (driver.executeScript) 中可用,但找不到 python.现在我让电脑按一千次结束键:

I am trying to scroll to the end of a page so that I can make all the data visible and extract it. I tried to find a command for it but it's available in java (driver.executeScript) but couldn't find for python. Right now I am making the computer press the end key thousand times:

while i<1000:
    scroll = driver.find_element_by_tag_name('body').send_keys(Keys.END)
    i+=1

我也试过 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 但它滚动到加载页面的末尾和同样的东西 END 键做.到达页面底部后,将加载下一个内容.但现在它不会再次滚动.

And I also tried driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") but it scrolls to the end of the loaded page and the same thing END key does. Once at the bottom of the page, next content loads. But now it doesn't scroll again.

我知道会有一个非常好的替代方案.

I know there will be a very nice alternative for this.

如何在 Python 中使用 selenium 滚动到页面末尾?

How do I scroll to the end of the page using selenium in Python?

推荐答案

好吧,我终于想出了一个解决方案:

Well I finally figured out a solution:

lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
    match=False
        while(match==False):
                lastCount = lenOfPage
                time.sleep(3)
                lenOfPage = driver.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
                if lastCount==lenOfPage:
                    match=True

这篇关于如何在 Python 中使用 selenium 滚动到页面末尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆