Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me make a comment on youtube
#11
OK i figured it out.
The elements change based on whether its clicked or not. So you have to click it to get contenteditable-root ID available. Which before its clicked is ID "placeholder-area".
    def find_comment_box(self):
        try:
            print("Passing URL to Browser...")
            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)
            for _ in range(3):
                bg.send_keys(Keys.PAGE_DOWN)
                time.sleep(.5)
            print("Fetching comment box...")
     
            self.comment_box = self.browser.find_element_by_id('placeholder-area')
            self.comment_box.click()
            time.sleep(2)
            
            #self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")
            self.comment_box = self.browser.find_element_by_id('contenteditable-root')
            self.comment_box.send_keys('test')
Also you can make your script quicker by using WebDriverWait instead of time.sleep(). This will wait until its ready instead of an arbitrary number you put into time.sleep which actually makes it much faster. Scrolling is the only time i use time.sleep.
Recommended Tutorials:
Reply
#12
(Jun-22-2019, 09:33 PM)metulburr Wrote: OK i figured it out. The elements change based on whether its clicked or not. So you have to click it to get contenteditable-root ID available. Which before its clicked is ID "placeholder-area".
 def find_comment_box(self): try: print("Passing URL to Browser...") 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) for _ in range(3): bg.send_keys(Keys.PAGE_DOWN) time.sleep(.5) print("Fetching comment box...") self.comment_box = self.browser.find_element_by_id('placeholder-area') self.comment_box.click() time.sleep(2) #self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]") self.comment_box = self.browser.find_element_by_id('contenteditable-root') self.comment_box.send_keys('test')
Also you can make your script quicker by using WebDriverWait instead of time.sleep(). This will wait until its ready instead of an arbitrary number you put into time.sleep which actually makes it much faster. Scrolling is the only time i use time.sleep.
thanks, i'm still struggling to get this comment thing worked out.
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 a critical error that broke the program!"
        self.browser = webdriver.Chrome(executable_path = '/Users/carsonrhodes/Desktop/Chrome Driver/chromedriver') #comment out to remove browser if isolating a method
        self.comment_box = " "
        self.comment = "assignment complete"

# logs user into browser so that they may comment on a video.
    def login(self):

        # URL for google login
        url = "https://accounts.google.com/servicelogin"

        try:
            print("Passing URL to Browser...")
            self.browser.get(url)
            print(" ")
            time.sleep(4)
            print("What is your Google email?")
            email = raw_input("") #uncomment to ask for user input
            print("What is your Google password?")
            password = raw_input("") #uncomment to ask for user input
            print("Passing infomation to Browser...")
            email_input_box = self.browser.find_element_by_id("identifierId")
            time.sleep(.5)
            email_input_box.send_keys(email, Keys.ENTER)
            time.sleep(6.5)
            password_input_box = self.browser.find_element_by_class_name("whsOnd")
            time.sleep(.5)
            password_input_box.send_keys(password, Keys.ENTER)

        except Exception as exception:
            print(self.error)
            print("there was an exception when passing information to browser: ")
            print(exception)

# fetches input URL from user
    def fetch_input(self):
        print("- ")
        print("What video do you want to comment on?")
        print("- ")
        self.url = raw_input("Enter URL: ") #sample url : https://www.youtube.com/watch?v=uRVusUSNd2E


# finds comment box so that the script can comment.
    def find_comment_box(self):
        try:
            print("Passing URL to Browser...")
            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)
            for _ in range(3):
                bg.send_keys(Keys.PAGE_DOWN)
                time.sleep(.5)
            print("loading webpage...")
            time.sleep(5.5)
            print("Fetching comment box...")
            try:
                self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")
                self.comment_box.click()
            except Exception as exception:
                #print(self.error)
                print("method 1 failure.")
                #print(exception)
            try:
                self.comment_box = self.browser.find_element_by_xpath("//div[@id='contenteditable-root']")
                self.comment_box.click()
            except Exception as exception:
                #print(self.error)
                print("method 2 failure.")
                #print(exception)
            try:
                self.comment_box = self.browser.find_element_by_xpath("//div[@id='labelAndInputContainer']")
                self.comment_box.click()
            except Exception as exception:
                #print(self.error)
                print("method 3 failure.")
                #print(exception)
            try:
                self.comment_box = self.browser.find_element_by_xpath('//*[@id="contenteditable-root"]')
                self.comment_box.click()
            except Exception as exception:
                #print(self.error)
                print("method 4 failure.")
                print(exception)
        except Exception as exception:
            print(self.error)
            print("unable to find comment box:")
            print(exception)

# enters comment into comment box on YouTube
    def enter_comment(self):
        try:
            self.comment_box.send_keys(self.comment)
        except Exception as internalerror:
            print("failed to successfully enter a comment:")
            print(internalerror)
            print(self.error)

Commenter = Commenter() #creates class (cannot call methods without calling class)

# code starts here:
Commenter.login()
Commenter.fetch_input()
Commenter.find_comment_box()
Commenter.enter_comment
any ideas?

(Jun-22-2019, 09:33 PM)metulburr Wrote: def find_comment_box(self):
    try:
        print("Passing URL to Browser...")
        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)
        for _ in range(3):
            bg.send_keys(Keys.PAGE_DOWN)
            time.sleep(.5)
        print("Fetching comment box...")
      
        self.comment_box = self.browser.find_element_by_id('placeholder-area')
        self.comment_box.click()
        time.sleep(2)
             
        #self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")
        self.comment_box = self.browser.find_element_by_id('contenteditable-root')
        self.comment_box.send_keys('test')
oh my bad i see you figured it out

(Jun-22-2019, 09:33 PM)metulburr Wrote: OK i figured it out. The elements change based on whether its clicked or not. So you have to click it to get contenteditable-root ID available. Which before its clicked is ID "placeholder-area".
 def find_comment_box(self): try: print("Passing URL to Browser...") 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) for _ in range(3): bg.send_keys(Keys.PAGE_DOWN) time.sleep(.5) print("Fetching comment box...") self.comment_box = self.browser.find_element_by_id('placeholder-area') self.comment_box.click() time.sleep(2) #self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]") self.comment_box = self.browser.find_element_by_id('contenteditable-root') self.comment_box.send_keys('test')
Also you can make your script quicker by using WebDriverWait instead of time.sleep(). This will wait until its ready instead of an arbitrary number you put into time.sleep which actually makes it much faster. Scrolling is the only time i use time.sleep.

Still not working

the method doesn't work because the click gets intercepted.

    def find_comment_box(self):
        try:
            print("Passing URL to Browser...")
            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)
            for _ in range(3):
                bg.send_keys(Keys.PAGE_DOWN)
                time.sleep(.5)
            print("Fetching comment box...")
            try:
                self.comment_box = self.browser.find_element_by_id('placeholder-area')
            except:
                pass
            try:
                self.comment_box = self.browser.find_element_by_xpath('//*[@id="placeholder-area"]')
            except:
                pass
            self.comment_box.click()
            time.sleep(2)

            #self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")
            self.comment_box = self.browser.find_element_by_id('contenteditable-root')
        except Exception as internalerror:
            print("failed to successfully find comment box")
            print(internalerror)
            print(self.error)
console error:
Reply
#13
I havent ever came across that error. So i cant figure out why exactly. When i run it, it inputs the string into the comment box.

You can try these methods from the first response, but converted to python syntax. That would be what i would try first.

On #3, you might have a slower connection than me and might want to implement a wait or longer time sleep.
Recommended Tutorials:
Reply
#14
(Jun-23-2019, 12:36 AM)metulburr Wrote: I havent ever came across that error. So i cant figure out why exactly. When i run it, it inputs the string into the comment box. You can try these methods from the first response, but converted to python syntax. That would be what i would try first. On #3, you might have a slower connection than me and might want to implement a wait or longer time sleep.
can you send me your working script?
Reply
#15
# 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('/home/metulburr/chromedriver')
        self.comment_box = " "
        self.login()
        time.sleep(5)
        self.find_comment_box()
        #<div id="contenteditable-root" contenteditable="true" dir="auto" class="style-scope yt-formatted-string" aria-label="Add a public comment..."></div>
        
    def login(self):
        url = "https://accounts.google.com/servicelogin"
        self.browser.get(url)
        email = USERNAME
        password = PASSWORD
        email_input_box = self.browser.find_element_by_id("identifierId")
        email_input_box.send_keys(email)
        next_btn = self.browser.find_element_by_class_name('RveJvd')
        next_btn.click()
        time.sleep(3)
        password_input_box = self.browser.find_element_by_class_name("whsOnd")
        time.sleep(.5)
        password_input_box.send_keys(password)
        next_btn = self.browser.find_element_by_class_name('RveJvd')
        next_btn.click()

        
    def find_comment_box2(self):

        print("Passing URL to Browser...")
        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)
        for _ in range(3):
            bg.send_keys(Keys.PAGE_DOWN)
            time.sleep(.5)

        self.comment_box = self.browser.find_element_by_id("commentbox")
        self.comment_box.send_keys('werwfdgsdf')
        
    def find_comment_box(self):
        try:
            print("Passing URL to Browser...")
            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)
            for _ in range(3):
                bg.send_keys(Keys.PAGE_DOWN)
                time.sleep(.5)
            print("Fetching comment box...")
     
            self.comment_box = self.browser.find_element_by_id('placeholder-area')
            self.comment_box.click()
            time.sleep(2)
            
            #self.comment_box = self.browser.find_element_by_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")
            self.comment_box = self.browser.find_element_by_id('contenteditable-root')
            self.comment_box.send_keys('test')
            
        except Exception as exception:
            print(self.error)
            print("unable to find comment box:")
            print(exception)
 
  
Commenter = Commenter() #creates class (cannot call methods without calling class.
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parsing comment tag ian 2 2,952 Jan-22-2018, 03:38 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020