如何在 python 中使用 selenium webdriver 滚动网页?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20986631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I scroll a web page using selenium webdriver in python?
提问by user2523364
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 friends. How can I scroll down in Selenium. I am using python.
我目前正在使用 selenium webdriver 解析 Facebook 用户好友页面并从 AJAX 脚本中提取所有 ID。但是我需要向下滚动才能找到所有朋友。如何在 Selenium 中向下滚动。我正在使用蟒蛇。
回答by lukeis
回答by OWADVL
You can use
您可以使用
driver.execute_script("window.scrollTo(0, Y)")
where Y is the height (on a fullhd monitor it's 1080). (Thanks to @lukeis)
其中 Y 是高度(在全高清显示器上是 1080)。(感谢@lukeis)
You can also use
你也可以使用
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
to scroll to the bottom of the page.
滚动到页面底部。
If you want to scroll to a page with infinite loading, like social network ones, facebook etc. (thanks to @Cuong Tran)
如果你想滚动到无限加载的页面,比如社交网络、facebook 等(感谢@Cuong 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
another method (thanks to Juanse) is, select an object and
另一种方法(感谢 Juanse)是,选择一个对象并
label.sendKeys(Keys.PAGE_DOWN);
回答by premonition
element=find_element_by_xpath("xpath of the li you are trying to access")
element.location_once_scrolled_into_view
this helped when I was trying to access a 'li' that was not visible.
当我尝试访问不可见的“li”时,这会有所帮助。
回答by Cuong Tran
If you want to scroll down to bottom of infinite page(like linkedin.com), you can use this code:
如果你想向下滚动到无限页面的底部(比如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
Reference: https://stackoverflow.com/a/28928684/1316860
回答by Bass Dee
None of these answers worked for me, at least not for scrolling down a facebook search result page, but I found after a lot of testing this solution:
这些答案都不适用于我,至少不适用于向下滚动 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
回答by Juanse
The easiest way i found to solve that problem was to select a label and then send:
我发现解决该问题的最简单方法是选择一个标签,然后发送:
label.sendKeys(Keys.PAGE_DOWN);
Hope it works!
希望它有效!
回答by LIU YUE
You can use send_keysto simulate an END(or PAGE_DOWN) key press(which normally scroll the page):
您可以使用send_keys模拟END(或PAGE_DOWN)按键(通常滚动页面):
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.END)
回答by Nick Brady
For my purpose, I wanted to scroll down more, keeping the windows position in mind. My solution was similar and used window.scrollY
为了我的目的,我想向下滚动更多,记住窗口位置。我的解决方案类似并使用window.scrollY
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
which will go to the current y scroll position + 200
这将转到当前 y 滚动位置 + 200
回答by Splarty
I was looking for a way of scrolling through a dynamic webpage, and automatically stopping once the end of the page is reached, and found this thread.
我正在寻找一种滚动动态网页的方法,并在到达页面末尾时自动停止,并找到了这个线程。
The post by @Cuong Tran, with one main modification, was the answer that I was looking for. I thought that others might find the modification helpful (it has a pronounced effect on how the code works), hence this post.
@Cuong Tran的帖子做了一个主要修改,就是我正在寻找的答案。我认为其他人可能会发现修改有用(它对代码的工作方式有明显的影响),因此这篇文章。
The modification is to move the statement that captures the last page height insidethe loop (so that each check is comparing to the previous page height).
修改是将捕获的最后一页高度的声明内循环(使每个检查比较前一页面的高度)。
So, the code below:
所以,下面的代码:
Continuously scrolls down a dynamic webpage (
.scrollTo()), only stopping when, for one iteration, the page height stays the same.
不断向下滚动动态网页 (
.scrollTo()),仅在一次迭代中页面高度保持不变时停止。
(There is another modification, where the break statement is inside another condition (in case the page 'sticks') which can be removed).
(还有另一个修改,其中 break 语句位于另一个可以删除的条件内(以防页面“粘住”)。
SCROLL_PAUSE_TIME = 0.5
while True:
# Get scroll height
### This is the difference. Moving this *inside* the loop
### means that it checks if scrollTo is still scrolling
last_height = driver.execute_script("return document.body.scrollHeight")
# 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:
# try again (can be removed)
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")
# check if the page height has remained the same
if new_height == last_height:
# if so, you are done
break
# if not, move on to the next loop
else:
last_height = new_height
continue
回答by sahaja nadendla
This is how you scroll down the webpage:
这是您向下滚动网页的方式:
driver.execute_script("window.scrollTo(0, 1000);")

