亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在python中使用selenium webdriver滾動網頁?

如何在python中使用selenium webdriver滾動網頁?

慕哥9229398 2019-08-09 16:41:13
如何在python中使用selenium webdriver滾動網頁?我目前正在使用selenium webdriver來解析Facebook用戶朋友頁面并從AJAX腳本中提取所有ID。但我需要向下滾動才能吸引所有朋友。如何在Selenium中向下滾動。我正在使用python。
查看完整描述

3 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

您可以使用

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 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


查看完整回答
反對 回復 2019-08-09
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

from selenium.webdriver.common.keys import Keyshtml = browser.find_element_by_tag_name('html')html.send_keys(Keys.END)

測試,它的工作原理


查看完整回答
反對 回復 2019-08-09
  • 3 回答
  • 0 關注
  • 749 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號