我目前使用硒webdriver解析通过facebook用户的朋友页面,并从AJAX脚本提取所有id。但我需要向下滚动来找到所有的朋友。如何向下滚动硒。我正在使用python。
当前回答
方法如下图所示:
在python中,你可以使用
driver.execute_script("window.scrollTo(0, Y)")
(Y为要滚动到的垂直位置)
其他回答
element=find_element_by_xpath("xpath of the li you are trying to access")
element.location_once_scrolled_into_view
当我试图进入一个不可见的“li”时,这很有帮助。
你可以使用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)
使用“send keys”方法滚动页面的循环:
pre_scroll_height = driver.execute_script('return document.body.scrollHeight;')
while True:
driver.find_element_by_tag_name('body').send_keys(Keys.END)
time.sleep(5)
post_scroll_height = driver.execute_script('return document.body.scrollHeight;')
print(pre_scroll_height, post_scroll_height)
if pre_scroll_height == post_scroll_height:
break
pre_scroll_height=post_scroll_height
这是你如何向下滚动网页:
driver.execute_script("window.scrollTo(0, 1000);")
ScrollTo()函数不再工作。这是我用过的,效果很好。
driver.execute_script("document.getElementById('mydiv').scrollIntoView();")
推荐文章
- 如何在Flask-SQLAlchemy中按id删除记录
- 在Python中插入列表的第一个位置
- Python Pandas只合并某些列
- 如何在一行中连接两个集而不使用“|”
- 从字符串中移除前缀
- 代码结束时发出警报
- 如何在Python中按字母顺序排序字符串中的字母
- 在matplotlib中将y轴标签添加到次要y轴
- 如何消除数独方块的凹凸缺陷?
- 为什么出现这个UnboundLocalError(闭包)?
- 使用Python请求的异步请求
- 如何检查一个对象是否是python中的生成器对象?
- 如何从Python包内读取(静态)文件?
- 如何计算一个逻辑sigmoid函数在Python?
- python: SyntaxError: EOL扫描字符串文字