I can’t get xpath from comment box on Youtube

Asked

Viewed 49 times

1

I need to be able to comment on Youtube, but XPATH does not work, even with the right parameters, I tried with ID and ELEMENT but I was not successful.

from selenium import webdriver
import keyboard
import time

#--------------pra comentar faça login no gmail inserindo os paramentros login_Gmail,password_Gmail.--------------#

driver = webdriver.Chrome(r"C:\Users\FAMILIA\Downloads\aaa\chromedriver.exe")
x = driver.get('https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin')
time.sleep(2)

login_Gmail = driver.find_element_by_xpath('//*[@id="identifierId"]').send_keys('SEU EMAIL GMAIL')
print('e-mail ok')
time.sleep(2)
keyboard.press('Enter')
time.sleep(2)
password_Gmail = driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input').send_keys('SUA SENHA GMAIL')
print('senha ok')
time.sleep(2)
keyboard.press('Enter')
print('login feito')
time.sleep(1)
x = driver.get('https://www.youtube.com/watch?v=9nV9GNjXFDo&list=RD9nV9GNjXFDo&start_radio=1')
time.sleep(3)
print('youtube video')
comment_New = driver.find_element_by_xpath('//*[@id="contenteditable-textarea"]').send_keys('TEXTO A SER COEMNTADO')  ###ESTA PARTE QUE ESTOUC COM PROBLEMAS###
keyboard.press('Enter')

1 answer

0


I did some tests and I was able to comment as follows:

First, for the comment field to appear, you need to scroll down the page a little. So I used:

driver.execute_script("window.scrollTo(0, 350)")

where 350 is the Y value to scroll down (in my case, from 0 to 1080, by the resolution of the monitor)

Then, for the comment to be enabled, you need to click on the element first. For this, I used:

driver.find_element_by_xpath('//*[@id="placeholder-area"]').click()

The final code went like this:

driver.execute_script("window.scrollTo(0, 350)")
time.sleep(3)
driver.find_element_by_xpath('//*[@id="placeholder-area"]').click()
driver.find_element_by_xpath('//*[@id="contenteditable-root"]').send_keys('Teste')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.