使用Selenium滚动页面的高效方法 [英] Efficient method to scroll though pages using Selenium

查看:290
本文介绍了使用Selenium滚动页面的高效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用Python中的Selenium Firefox Driver进行滚动编码,是否有顺利滚动页面的顺序?页面加载时可能比正常页面大20倍。



我需要多次上下滚动,而不是错过页面的任何部分。

目前的方法不是有效的,有时不会滚动到页面的某个部分。
以下是我目前的代码:

$ p $ browser = webdriver.Firefox()
browser.get(link )

browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 3.4);)
time.sleep(0.2)
browser.execute_script( window.scrollTo(0,document.body.scrollHeight / 3.5);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 3.7) ;)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 3.8);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 4);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0, document.body.scrollHeight / 4.2);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 4.3);)
time.sleep(0.2)
browser.execute_script(window.s crollTo(0,document.body.scrollHeight / 4.5);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 4.7); )
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 4.9);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 5.2);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document。 body.scrollHeight / 5.1);)
time.sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 5.8);)
time .sleep(0.2)
browser.execute_script(window.scrollTo(0,document.body.scrollHeight / 3.7);)
time.sleep(0.2)
browser.execute_script( window.scrollTo(0,document.body.scrollHeight / 50);)
time.sleep(0.2)


解决方案

您可以使用这个i f使用Python中的Selenium ...

  scheight = .1 
while scheight< 9.9:
driver.execute_script(window.scrollTo(0,document.body.scrollHeight /%s);%scheight)
scheight + = .01

或者沿着这些线。如果刹车,您可以添加来捕获中特定 scheight 位置处的数据,而。只要让 + = 或迭代任何你喜欢的。我用.01所以我可以直观地看到它滚动,否则你会需要超时,它会出现波涛汹涌。取决于你需要什么。


I'm currently hard coding a scrolling using Selenium, Firefox Driver in Python, is there anyway for it to scroll smoothly up and down the page? The page can grow 20 times bigger than normal as the images in the page loads.

I need to scroll up and down multiple times , not missing out any part of the page.

The current method isnt efficient and sometimes doesnt scroll to some part of the page. The following is my current code

 browser = webdriver.Firefox()
browser.get(link)

browser.execute_script("window.scrollTo(0, document.body.scrollHeight/3.4);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/3.5);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/3.7);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/3.8);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4.2);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4.3);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4.5);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4.7);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/4.9);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/5.2);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/5.1);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/5.8);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/3.7);")
        time.sleep(0.2)
        browser.execute_script("window.scrollTo(0, document.body.scrollHeight/50);")
        time.sleep(0.2)

解决方案

You can use this if using Selenium in Python...

scheight = .1
while scheight < 9.9:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight/%s);" % scheight)
    scheight += .01

Or something along those lines. You can add if brakes to capture data at specific scheight locations within the while. Just make the += or iteration whatever you like. I used .01 so I could visually see it scroll, otherwise you would need timeouts and it would appear choppy. Depends on what you need.

这篇关于使用Selenium滚动页面的高效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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