(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".thanks, i'm still struggling to get this comment thing worked out.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.
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_commentany ideas?
(Jun-22-2019, 09:33 PM)metulburr Wrote: def find_comment_box(self):oh my bad i see you figured it out
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')
(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: