我目前正在使用selenium webdriver来解析Facebook用户朋友页面并从AJAX脚本中提取所有ID.但我需要向下滚动才能吸引所有朋友.如何在Selenium中向下滚动.我正在使用python.
您可以使用
driver.execute_script("window.scrollTo(0, Y)")
其中Y是高度(在全高清监视器上它是1080).(感谢@lukeis)
你也可以使用
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
滚动到页面底部.
如果你想scrool到一个无限加载的页面,如社交网络,Facebook等(感谢@Cong 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
与此处显示的方法相同:
在python中你可以使用
driver.execute_script("window.scrollTo(0, Y)")
(Y是您要滚动到的垂直位置)
from selenium.webdriver.common.keys import Keys html = browser.find_element_by_tag_name('html') html.send_keys(Keys.END)
测试,它的工作原理
如果要向下滚动到无限页面的底部(如linkedin.com),可以使用以下代码:
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
参考:https://stackoverflow.com/a/28928684/1316860
这些答案对我都不起作用,至少不是向下滚动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("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
我发现解决该问题的最简单方法是选择一个标签,然后发送:
label.sendKeys(Keys.PAGE_DOWN);
希望它能起作用!
element=find_element_by_xpath("xpath of the li you are trying to access") element.location_once_scrolled_into_view
当我试图访问一个看不见的'li'时,这有帮助.
出于我的目的,我想向下滚动更多,同时牢记窗口的位置。我的解决方案是相似的,并使用window.scrollY
driver.execute_script("window.scrollTo(0, window.scrollY + 200)")
它将转到当前的y滚动位置+ 200