This method is supposed to find the comment box on youtube, right now, my scrolling method does not seem to be working. I can't seem to figure out why. Any ideas?
def find_comment_box(self):
try:
self.browser.get(self.url)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = self.browser.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
self.browser.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 = self.browser.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
if last_height != new_height:
pass
print("hello")
self.comment_box = self.browser.find_element_by_id("contenteditable-root")
except Exception as internalerror:
print(internalerror)
print(self.error)
Expain in more detail? Do you get an error? Is it in a different window? Does it scroll at least once, or not at all? Try a timesleep right after scrolling before loading the element ID to attempt to see if it does anything.
The best way to get an answer is to make an example script that goes to youtube and attempts to scroll so wecan just run it
(Jun-16-2019, 04:49 PM)metulburr Wrote: [ -> ]def find_comment_box(self):
try:
self.browser.get(self.url)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = self.browser.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
self.browser.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 = self.browser.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
if last_height != new_height:
pass
print("hello")
self.comment_box = self.browser.find_element_by_id("contenteditable-root")
except Exception as internalerror:
print(internalerror)
print(self.error)
going to make you guys a script that does this for debugging, thank you!
(Jun-16-2019, 04:49 PM)metulburr Wrote: [ -> ]Expain in more detail? Do you get an error? Is it in a different window? Does it scroll at least once, or not at all? Try a timesleep right after scrolling before loading the element ID to attempt to see if it does anything. The best way to get an answer is to make an example script that goes to youtube and attempts to scroll so wecan just run it
# imports
import random
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class Commenter:
# declares variables for commentor script
def __init__(self):
self.error = "[!] There was an error!"
self.url = "https://www.youtube.com/watch?v=SEpmYLu-CCA"
self.browser = webdriver.Chrome(executable_path = '/Users/carsonrhodes/Desktop/Chrome Driver/chromedriver')
self.comment_box = " "
# finds comment box so that the script can comment.
def find_comment_box(self):
try:
self.browser.get(self.url)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = self.browser.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
self.browser.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 = self.browser.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
if last_height != new_height:
pass
print("hello")
self.comment_box = self.browser.find_element_by_id("contenteditable-root")
except Exception as internalerror:
print(internalerror)
print(self.error)
Commenter = Commenter() #creates class (cannot call methods without calling class.
Commenter.find_comment_box()
this should be a working script.
Im not sure why this doesnt work. But page down does work on this site
class Commenter:
# declares variables for commentor script
def __init__(self):
self.error = "[!] There was an error!"
self.url = "https://www.youtube.com/watch?v=SEpmYLu-CCA"
self.browser = webdriver.Chrome('/home/metulburr/chromedriver')
self.comment_box = " "
self.browser.get(self.url)
bg = self.browser.find_element_by_css_selector('body')
for _ in range(3):
bg.send_keys(Keys.PAGE_DOWN)
time.sleep(.5)
This gets access to the input box of commenting
(Jun-20-2019, 08:20 PM)metulburr Wrote: [ -> ] bg = self.browser.find_element_by_css_selector('body')
for _ in range(3):
bg.send_keys(Keys.PAGE_DOWN)
time.sleep(.5)
elegent and beautiful solution thanks!