如何在python中使用SeleniumWebDriver滚动网页?
我目前正在使用SeleniumWebDriver解析facebook用户好友页面,并从AJAX脚本中提取所有ID。但我需要向下滚动才能找到所有的朋友。如何在Selenium中向下滚动。我正在使用python。与所示方法相同: 在python中,您只需使用如何在python中使用SeleniumWebDriver滚动网页?,python,selenium,selenium-webdriver,automated-tests,Python,Selenium,Selenium Webdriver,Automated Tests,我目前正在使用SeleniumWebDriver解析facebook用户好友页面,并从AJAX脚本中提取所有ID。但我需要向下滚动才能找到所有的朋友。如何在Selenium中向下滚动。我正在使用python。与所示方法相同: 在python中,您只需使用 driver.execute_script("window.scrollTo(0, Y)") (Y是要滚动到的垂直位置)您可以使用 driver.execute_script("window.scrollTo(0, Y)") driver
driver.execute_script("window.scrollTo(0, Y)")
(Y是要滚动到的垂直位置)您可以使用
driver.execute_script("window.scrollTo(0, Y)")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
其中Y是高度(在全高清显示器上为1080)。(感谢@lukeis)
您也可以使用
driver.execute_script("window.scrollTo(0, Y)")
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
滚动至页面底部。
如果您希望滚动到无限加载的页面,如社交网络、facebook等(感谢@Cuong-Tran)
另一种方法(感谢Juanse)是,选择一个对象并
label.sendKeys(Keys.PAGE_DOWN);
当我试图访问一个不可见的“li”时,这很有帮助。如果您想向下滚动到无限页面的底部(如),可以使用以下代码:
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
参考资料:这些答案对我来说都不管用,至少不适用于向下滚动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);
希望它能起作用 您可以使用模拟(通常滚动页面):
出于我的目的,我想向下滚动更多,记住窗口的位置。我的解决方案与之类似,使用了
窗口。滚动
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
它将转到当前的y滚动位置+200我正在寻找一种滚动动态网页的方法,并在到达页面末尾时自动停止,并找到了此线程
我一直在寻找的答案是作者的帖子,其中有一个主要的修改。我认为其他人可能会发现修改很有帮助(它对代码的工作方式有明显的影响),因此本文发表了这篇文章
修改是将捕获最后一页高度的语句移动到循环内部(以便每次检查都与上一页高度进行比较)
因此,代码如下:
连续向下滚动动态网页(.scrollTo()
),仅在一次迭代中页面高度保持不变时停止
(还有另一个修改,其中break语句在另一个条件内(如果页面“粘住”),可以删除)
以下是您向下滚动网页的方式:
driver.execute_script("window.scrollTo(0, 1000);")
使用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
滚动加载页面。例如:中号、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()
此代码滚动到底部,但不需要每次都等待。它将持续滚动,然后在底部停止(或超时)
这比每次等待响应0.5-3秒要快得多,而响应可能需要0.1秒如果要在特定视图/帧中滚动(WebElement),只需将“body”替换为要滚动的特定元素即可。在下面的示例中,我通过“getElementById”获取该元素:
self.driver.execute_script('window.scrollTo(0, document.getElementById("page-manager").scrollHeight);')
例如,YouTube上就是这种情况,ScrollTo()
函数不再工作了。这是我用过的,效果很好
driver.execute_script("document.getElementById('mydiv').scrollIntoView();")
它适用于我的案例。下面是一个示例selenium代码片段,您可以将其用于此类目的。它会转到“Enumerate python tutorial”(枚举python教程)上youtube搜索结果的url并向下滚动,直到找到标题为“Enumerate python tutorial(2020)”的视频
您可以使用send_键模拟向下翻页键(通常滚动页面):
插入这一行driver.execute_脚本(“window.scrollBy(0925)”,“”)
驱动程序的可能副本。execute_脚本(f“window.scrollTo(0,{2**127}”))这很好。对于试图在instagram上使用此功能的任何人,您可能需要首先使用ActionChains在“加载更多”按钮上添加选项卡,然后应用Cuong Tran的解决方案。。。至少这对我有用。谢谢你的回答!我想做的是在instagram中滚动到页面底部,然后抓取页面的整个html。selenium中是否有一个函数,在我滚动到底部后,我可以将last_height作为输入并获取整个页面的html?滚动暂停时间不同,对我来说大约需要2秒。它工作正常,但非常慢(至少对我来说)。我发现,如果将滚动\u暂停\u时间
设置为2
,效果很好,向下滚动速度快了100倍。很好,你能解释一下滚动高度
,它的意思是什么,通常是如何工作的?然后你会如何使用变量“last\u height”?我的代码中有类似的东西,浏览器正在向下滚动。但是,当我查看正在刮取的数据时,它只从第一页刮取数据k次,“k”是浏览器向下滚动的次数。@JasonGoal希望这会有所帮助:driver.execute\u script
可以与平滑滚动()相结合,以模仿更人性化的行为“通过xpath查找元素”是一个驱动程序函数还是什么,“.location\u一旦滚动到视图中”返回错误NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“/*[@id=“timeline medley”]/div/div[2]/div[1]}还有一件事。之所以在不使用()
的情况下调用location\u once\u scrolled\u into\u view
,是因为location\u once\u scrolled\u into\u view
是Python<
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path='chromedriver.exe')
driver.get('https://example.com')
pre_scroll_height = driver.execute_script('return document.body.scrollHeight;')
run_time, max_run_time = 0, 1
while True:
iteration_start = time.time()
# Scroll webpage, the 100 allows for a more 'aggressive' scroll
driver.execute_script('window.scrollTo(0, 100*document.body.scrollHeight);')
post_scroll_height = driver.execute_script('return document.body.scrollHeight;')
scrolled = post_scroll_height != pre_scroll_height
timed_out = run_time >= max_run_time
if scrolled:
run_time = 0
pre_scroll_height = post_scroll_height
elif not scrolled and not timed_out:
run_time += time.time() - iteration_start
elif not scrolled and timed_out:
break
# closing the driver is optional
driver.close()
self.driver.execute_script('window.scrollTo(0, document.getElementById("page-manager").scrollHeight);')
driver.execute_script("document.getElementById('mydiv').scrollIntoView();")
driver.execute_script("document.getElementById('your ID Element').scrollIntoView();")
driver.get('https://www.youtube.com/results?search_query=enumerate+python')
target = driver.find_element_by_link_text('Enumerate python tutorial(2020).')
target.location_once_scrolled_into_view
from selenium.webdriver.common.keys import Keys
html = driver.find_element_by_tag_name('html')
html.send_keys(Keys.PAGE_DOWN)