Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me make a comment on youtube
#1
# imports
import random
import time
import csv
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') 
        self.comment_box = " "
        self.comment = " "
    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
    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_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")

        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)

# code starts here:
Commenter.login()
Commenter.fetch_input()
Commenter.find_comment_box()
The issue with this code is that the ID of the comment box keeps changing, which means the comment box is unidentifiable to the find_comment_box method.

Any suggestions?



Thanks in advance,
Carson.
Reply
#2
Are you trying to make a comment without being logged in? Because i dont think you can. If you click the input box, you go directly to a login screen. Its possible the guest input box might change, whereas the logged in one is easier to obtain because its not shown to guests.

the input box not being logged in
<div id="placeholder-area" class="style-scope ytd-comment-simplebox-renderer">
      <yt-formatted-string id="simplebox-placeholder" role="textbox" tabindex="0" class="style-scope ytd-comment-simplebox-renderer">Add a public comment...</yt-formatted-string>
    </div>
Thus it cannot find contenteditable-root or contenteditable-textarea (which does show for being logged in)
Recommended Tutorials:
Reply
#3
(Jun-20-2019, 10:28 PM)metulburr Wrote: Are you trying to make a comment without being logged in? Because i dont think you can. If you click the input box, you go directly to a login screen. Its possible the guest input box might change, whereas the logged in one is easier to obtain because its not shown to guests. the input box not being logged in
<div id="placeholder-area" class="style-scope ytd-comment-simplebox-renderer"> <yt-formatted-string id="simplebox-placeholder" role="textbox" tabindex="0" class="style-scope ytd-comment-simplebox-renderer">Add a public comment...</yt-formatted-string> </div>
Thus it cannot find contenteditable-root or contenteditable-textarea (which does show for being logged in)
I'm logging into google before I navigate to a youtube URL.
Reply
#4
Thats not in your code though. (im too busy to write up code myself to log in)
Recommended Tutorials:
Reply
#5
(Jun-21-2019, 12:22 AM)metulburr Wrote: Thats not in your code though. (im too busy to write up code myself to log in)
add this for a login method:
    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("")
            print("What is your Google password?")
            password = raw_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)
Reply
#6
suggestions?
Reply
#7
I keep coming across the same issue you have. Im too busy today to look into it deeper.
Recommended Tutorials:
Reply
#8
(Jun-21-2019, 07:11 PM)metulburr Wrote: I keep coming across the same issue you have. Im too busy today to look into it deeper.
thank you!
Reply
#9
Bump.

tried a new strategy but this seems to not work either, this is the closest to working however.

    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_xpath("//yt-formatted-string[contains(text(),'Add a public comment...')]")

        except Exception as exception:
            print(self.error)
            print("unable to find comment box:")
            print(exception)
Reply
#10
Try finding the text box in all the other ways and hopefully one will work for you - https://www.techbeamers.com/locate-eleme...ium-python
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  parsing comment tag ian 2 2,981 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