# 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.
