我目前使用硒webdriver解析通过facebook用户的朋友页面,并从AJAX脚本提取所有id。但我需要向下滚动来找到所有的朋友。如何向下滚动硒。我正在使用python。
当前回答
如果你想在一个特定的视图/帧(WebElement)内滚动,你只需要用你想要滚动的特定元素替换“body”。在下面的例子中,我通过“getElementById”获得该元素:
self.driver.execute_script('window.scrollTo(0, document.getElementById("page-manager").scrollHeight);')
这就是YouTube上的例子……
其他回答
使用“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
下面是我编写的一个缓慢向下滚动到targets元素的方法
你可以将CSS选择器中元素的y号位置传递给它
它就像我们通过鼠标滚轮一样滚动
一旦这个方法被调用,你用相同的驱动对象再次调用它,但是使用新的目标元素,它将在元素存在的任何地方向上/向下滚动
def slow_scroll_to_element(self, driver, element_selector=None, target_yth_location=None):
current_scroll_position = int(driver.execute_script("return window.scrollY"))
if element_selector:
target_yth_location = int(driver.execute_script("return document.querySelector('{}').getBoundingClientRect()['top'] + window.scrollY".format(element_selector)))
scrollSpeed = 100 if target_yth_location-current_scroll_position > 0 else -100
def chunks(a, n):
k, m = divmod(len(a), n)
return (a[i*k+min(i, m):(i+1)*k+min(i+1, m)] for i in range(n))
for l in list(chunks(list(range(current_scroll_position, target_yth_location, scrollSpeed)) + list([target_yth_location+(-scrollSpeed if scrollSpeed > 0 else scrollSpeed)]), 3)):
for pos in l:
driver.execute_script("window.scrollTo(0, "+str(pos)+");")
time.sleep(0.1)
time.sleep(random.randint(1,3))
你可以使用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)
你可以使用
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);
滚动加载页面。例如:medium, quora等
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight-1000);")
# Wait to load the page.
driver.implicitly_wait(30) # seconds
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
# sleep for 30s
driver.implicitly_wait(30) # seconds
driver.quit()
推荐文章
- 在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数据框架列