我目前使用硒webdriver解析通过facebook用户的朋友页面,并从AJAX脚本提取所有id。但我需要向下滚动来找到所有的朋友。如何向下滚动硒。我正在使用python。
当前回答
这些答案都不适合我,至少不适合滚动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
其他回答
driver.execute_script("document.getElementById('your ID Element').scrollIntoView();")
这对我的案子起作用了。
当使用youtube时,浮动元素给出值“0”作为滚动高度 与其使用return document。body。scrollHeight"尝试使用这个"return document。documentelement。scrollHeight" 根据您的网速调整滚动暂停时间 否则它将只运行一次,然后在此之后中断。
SCROLL_PAUSE_TIME = 1
# Get scroll height
"""last_height = driver.execute_script("return document.body.scrollHeight")
this dowsnt work due to floating web elements on youtube
"""
last_height = driver.execute_script("return document.documentElement.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0,document.documentElement.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.documentElement.scrollHeight")
if new_height == last_height:
print("break")
break
last_height = new_height
这些答案都不适合我,至少不适合滚动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
我发现解决这个问题的最简单的方法是选择一个标签,然后发送:
label.sendKeys(Keys.PAGE_DOWN);
希望它有用!
出于我的目的,我想要更多地向下滚动,记住窗口的位置。我的解决方案类似,使用window.scrollY
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
哪个会到当前的y轴滚动位置+ 200
推荐文章
- 在Python中创建一个初始容量的列表
- Pylint在Visual Studio代码中“未解决的导入”错误
- Matplotlib错误-没有名为tkinter的模块
- 0到1之间的随机数?
- 使用Boto3将S3对象作为字符串打开
- "pip install——editable ./" vs "python setup.py develop"
- Pandas:索引数据帧时的多个条件-意外行为
- 如何更改Django应用程序的名称?
- 如何在python抽象类中创建抽象属性?
- “克隆”行或列向量
- 在python shell中按方向键时看到转义字符
- Cypress:只运行一个测试
- 在pip install中方括号是什么意思?
- 使用Matplotlib以非阻塞的方式绘图
- 使用sklearn缩放Pandas数据框架列