如何在python中使用selenium webdriver滾動網(wǎng)頁?我目前正在使用selenium webdriver來解析Facebook用戶朋友頁面并從AJAX腳本中提取所有ID。但我需要向下滾動才能吸引所有朋友。如何在Selenium中向下滾動。我正在使用python。
3 回答

阿晨1998
TA貢獻2037條經(jīng)驗 獲得超6個贊
您可以使用
driver.execute_script("window.scrollTo(0, Y)")
其中Y是高度(在全高清監(jiān)視器上它是1080)。(感謝@lukeis)
你也可以使用
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
滾動到頁面底部。
如果你想scrool到一個無限加載的頁面,如社交網(wǎng)絡,F(xiàn)acebook等(感謝@Cong Tran)
SCROLL_PAUSE_TIME = 0.5# Get scroll heightlast_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

拉風的咖菲貓
TA貢獻1995條經(jīng)驗 獲得超2個贊
from selenium.webdriver.common.keys import Keyshtml = browser.find_element_by_tag_name('html')html.send_keys(Keys.END)
測試,它的工作原理
添加回答
舉報
0/150
提交
取消