如何使用硒在Youtube中向下滚动?

时间:2019-03-28 14:55:46

标签: javascript python selenium selenium-webdriver selenium-chromedriver

我想在Youtube中启用需要向下滚动的注释框。
这是我现在正在做的事情:-

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=l5LfjYmNEJs&t=160s")

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

但是它只会向下滚动一点,但滚动幅度不足以启用注释框。
我该怎么办?

3 个答案:

答案 0 :(得分:2)

我找到了一种解决方案。看看是否有帮助。Intent intent = new Intent("com.instagram.share.ADD_TO_FEED"); //feed intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setDataAndType(image, "image/jpeg"); Uri image = getImageUri(); Activity activity = getActivity(); if (activity.getPackageManager().resolveActivity(intent, 0) != null) { activity.startActivityForResult(intent, 0); } 需要降低循环时的速度。

time.sleep

答案 1 :(得分:0)

KunduK的答案很棒!但对 Firefox 来说,这对我来说并不好,它可以获取频道的所有视频。因此,我修改了 JavaScript 部分

 height = driver.execute_script("return document.documentElement.scrollHeight(window.innerHeight + window.scrollY);")

答案 2 :(得分:0)

不幸的是,这个答案对我没有用。但这行得通。

from selenium import webdriver
import time

driver = webdriver.Chrome()
driver.get("https://www.youtube.com/c/klikklak/videos")

while True:
    scroll_height = 2000
    document_height_before = driver.execute_script("return 
    document.documentElement.scrollHeight")
    driver.execute_script(f"window.scrollTo(0, {document_height_before 
    + scroll_height});")
    time.sleep(1.5)
    document_height_after = driver.execute_script("return 
    document.documentElement.scrollHeight")
    if document_height_after == document_height_before:
        break