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


当前回答

我正在寻找一种滚动浏览动态网页的方法,并在到达页面末尾时自动停止,并找到了这个线程。

@Cuong Tran的这篇文章,有一个主要的修改,是我一直在寻找的答案。我认为其他人可能会发现这个修改很有帮助(它对代码的工作方式有明显的影响),因此写了这篇文章。

修改是将捕获最后一页高度的语句移动到循环内部(以便每个检查都与前一页高度进行比较)。

所以,下面的代码:

连续向下滚动一个动态网页(. scrollto()),只有在一次迭代中,页面高度保持相同时才停止。

(还有另一个修改,其中break语句在另一个条件(以防页面“卡住”)中,可以删除)。

    SCROLL_PAUSE_TIME = 0.5


    while True:

        # Get scroll height
        ### This is the difference. Moving this *inside* the loop
        ### means that it checks if scrollTo is still scrolling 
        last_height = driver.execute_script("return document.body.scrollHeight")

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

            # try again (can be removed)
            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")

            # check if the page height has remained the same
            if new_height == last_height:
                # if so, you are done
                break
            # if not, move on to the next loop
            else:
                last_height = new_height
                continue

其他回答

如果你想在一个特定的视图/帧(WebElement)内滚动,你只需要用你想要滚动的特定元素替换“body”。在下面的例子中,我通过“getElementById”获得该元素:

self.driver.execute_script('window.scrollTo(0, document.getElementById("page-manager").scrollHeight);')

这就是YouTube上的例子……

出于我的目的,我想要更多地向下滚动,记住窗口的位置。我的解决方案类似,使用window.scrollY

driver.execute_script("window.scrollTo(0, window.scrollY + 200)")

哪个会到当前的y轴滚动位置+ 200

滚动到元素:使用下面的代码找到元素并滚动。

scroll_element = driver.find_element(By.XPATH, "your element xpath")
driver.execute_script("arguments[0].scrollIntoView();", scroll_element)

下面是我编写的一个缓慢向下滚动到targets元素的方法

你可以将CSS选择器中元素的y号位置传递给它

它就像我们通过鼠标滚轮一样滚动

一旦这个方法被调用,你用相同的驱动对象再次调用它,但是使用新的目标元素,它将在元素存在的任何地方向上/向下滚动

def slow_scroll_to_element(self, driver, element_selector=None, target_yth_location=None):
    current_scroll_position = int(driver.execute_script("return window.scrollY"))
    
    if element_selector:
        target_yth_location = int(driver.execute_script("return document.querySelector('{}').getBoundingClientRect()['top'] + window.scrollY".format(element_selector)))
    
    scrollSpeed = 100 if target_yth_location-current_scroll_position > 0 else -100

    def chunks(a, n):
        k, m = divmod(len(a), n)
        return (a[i*k+min(i, m):(i+1)*k+min(i+1, m)] for i in range(n))
    
    for l in list(chunks(list(range(current_scroll_position, target_yth_location, scrollSpeed)) + list([target_yth_location+(-scrollSpeed if scrollSpeed > 0 else scrollSpeed)]), 3)):
        for pos in l:
            driver.execute_script("window.scrollTo(0, "+str(pos)+");")
            time.sleep(0.1)
        time.sleep(random.randint(1,3))

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