1

I want to enable the Comment Box in Youtube for which I need to scroll down.
Here is what I am doing right now:-

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);")

But it will only scroll down just a little bit but not down enough to enable the comment box.
What should I do?

luciferchase
  • 559
  • 1
  • 10
  • 24
  • Possible duplicate https://stackoverflow.com/questions/20986631/how-can-i-scroll-a-web-page-using-selenium-webdriver-in-python – KunduK Mar 28 '19 at 15:00
  • @KajalKundu I have seen that answer before this question. My problem is that the above code works in almost every site except youtube. – luciferchase Mar 28 '19 at 15:02
  • This is more of a Javascript question. Perhaps you can try: `driver.execute_script("document.getElementById("footer-links-secondary").scrollIntoView();")` – Ardweaden Mar 28 '19 at 15:07
  • @Ardweaden I ran the code it gives the following error - `selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'scrollIntoView' of null` – luciferchase Mar 28 '19 at 15:12
  • Ah, sorry, I have tried it on the main page instead of the video page. There might not be an element with that id on the latter one. How about `driver.execute_script("document.getElementById("footer-links").scrollIntoView();")`? – Ardweaden Mar 28 '19 at 15:17
  • @Ardweaden It is still giving the same error. I think the problem is with `scrollIntoView`? – luciferchase Mar 28 '19 at 15:23
  • No, the issue is that there isn't an element like that. I checked with Selenium on my computer and I can see it doesn't have a footer. This would work with the old youtube layout. – Ardweaden Mar 28 '19 at 15:43

3 Answers3

6

I have found one solution.See if this helps.time.sleep is required to slowdown while loop.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://www.youtube.com/watch?v=l5LfjYmNEJs&t=160s")

while(True):
    height = driver.execute_script("return document.body.scrollHeight")
    time.sleep(1)
    driver.find_element_by_tag_name('body').send_keys(Keys.END)
    if int(height)==0:
        break
KunduK
  • 32,888
  • 5
  • 17
  • 41
1

unforunately answer didn't work for me. But this worked.

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
0

KunduK's answer is awesome! but did not better for me in Firefox to get all videos of a channel. So I modified the JavaScript part

 height = driver.execute_script("return document.documentElement.scrollHeight(window.innerHeight + window.scrollY);")
Bashar
  • 453
  • 5
  • 14