我目前使用硒webdriver解析通过facebook用户的朋友页面,并从AJAX脚本提取所有id。但我需要向下滚动来找到所有的朋友。如何向下滚动硒。我正在使用python。


当前回答

医生说 类ActionChains完成工作:

from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Firefox()
action_chains = ActionChains(driver)
action_chains.scroll(x: int, y: int, delta_x: int, delta_y: int, duration: int = 0, origin: str = 'viewport').perform()

其他回答

如果你想滚动到无限页面的底部(如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

insert this line driver.execute_script("window.scrollBy(0,925)", "")

我发现解决这个问题的最简单的方法是选择一个标签,然后发送:

label.sendKeys(Keys.PAGE_DOWN);

希望它有用!

这是你如何向下滚动网页:

driver.execute_script("window.scrollTo(0, 1000);")

ScrollTo()函数不再工作。这是我用过的,效果很好。

driver.execute_script("document.getElementById('mydiv').scrollIntoView();")