我目前使用硒webdriver解析通过facebook用户的朋友页面,并从AJAX脚本提取所有id。但我需要向下滚动来找到所有的朋友。如何向下滚动硒。我正在使用python。
当前回答
只是目前提供的解决方案的一个小变化:有时在刮痧中你必须满足以下要求:
一步一步地滚动。否则,如果你总是跳到底部,一些元素只作为容器/div加载,但它们的内容没有加载,因为它们从来都不可见(因为你直接跳到底部); 为加载内容留出足够的时间; 这不是一个无限滚动的页面,有一个终点,你必须确定什么时候到达终点;
下面是一个简单的实现:
from time import sleep
def keep_scrolling_to_the_bottom():
while True:
previous_scrollY = my_web_driver.execute_script( 'return window.scrollY' )
my_web_driver.execute_script( 'window.scrollBy( 0, 230 )' )
sleep( 0.4 )
if previous_scrollY == my_web_driver.execute_script( 'return window.scrollY' ):
print( 'job done, reached the bottom!' )
break
测试和工作在Windows 7 x64, Python 3.8.0, selenium 4.1.3,谷歌Chrome 107.0.5304.107,物业租赁网站。
其他回答
driver.execute_script("document.getElementById('your ID Element').scrollIntoView();")
这对我的案子起作用了。
你可以使用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)
insert this line driver.execute_script("window.scrollBy(0,925)", "")
你可以使用
driver.execute_script("window.scrollTo(0, Y)")
其中Y是高度(在全高清显示器上是1080)。(感谢@lukeis)
你也可以使用
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
滚动到页面底部。
如果你想滚动到一个无限加载的页面,比如社交网络,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
另一种方法(感谢Juanse)是,选择一个对象和
label.sendKeys(Keys.PAGE_DOWN);
我正在寻找一种滚动浏览动态网页的方法,并在到达页面末尾时自动停止,并找到了这个线程。
@Cuong Tran的这篇文章,有一个主要的修改,是我一直在寻找的答案。我认为其他人可能会发现这个修改很有帮助(它对代码的工作方式有明显的影响),因此写了这篇文章。
修改是将捕获最后一页高度的语句移动到循环内部(以便每个检查都与前一页高度进行比较)。
所以,下面的代码:
连续向下滚动一个动态网页(. scrollto()),只有在一次迭代中,页面高度保持相同时才停止。
(还有另一个修改,其中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
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?
- 如何计算一个逻辑sigmoid函数在Python?
- python: SyntaxError: EOL扫描字符串文字