Python Selenium does not perform the send_keys function

Asked

Viewed 1,350 times

-2

I’m trying to make a bot to comment alone on youtube video, it’s working normally, until it’s time to type the comment. I have already researched everything that is place and I found nothing that helps me with this. If anyone can help I thank you. Follow the code:

from selenium import webdriver
import time


class botComentario:
def __init__(self):
    self.driver = webdriver.Chrome('C:/dev/chromedriver')
    self.username = "email"
    self.senha = "senha"
    self.comentario = "comentario"
    options = webdriver.ChromeOptions()

    options.add_argument('lang=pt-br')

def acessarPagina(self):
    self.driver.get('http://www.youtube.com.br')
    time.sleep(3)
    login = self.driver.find_element_by_xpath('//*[@id="buttons"]/ytd-button-renderer/a')
    time.sleep(1)
    login.click()
    time.sleep(3)
    logar = self.driver.find_element_by_xpath('//*[@id="identifierId"]')
    time.sleep(1)
    logar.click()
    logar.send_keys(self.username)
    conectar = self.driver.find_element_by_xpath('//*[@id="identifierNext"]/span')
    conectar.click()
    time.sleep(1)
    senha = self.driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input')
    time.sleep(1)
    senha.click()
    senha.send_keys(self.senha)
    time.sleep(1)
    conectar = self.driver.find_element_by_xpath('//*[@id="passwordNext"]/span')
    conectar.click()
    self.driver.get('https://www.youtube.com/watch?v=ZWmuR5H4PlU')
    time.sleep(3)
    login = self.driver.find_element_by_xpath('//*[@id="buttons"]/ytd-button-renderer/a')
    login.click()
    time.sleep(3)
    self.driver.execute_script("window.scrollTo(0, 4000)")
    time.sleep(5)

    sessao = 0
    while sessao == 0:
        time.sleep(5)
        comentario = self.driver.find_element_by_xpath('//*[@id="simplebox-placeholder"]')
        self.driver.execute_script("document.getElementById('simplebox-placeholder').click()")
        comentario.send_keys(self.comentario)
        time.sleep(5)
        time.sleep(2)
        btncoment = self.driver.find_element_by_xpath('//*[@id="submit-button"]/a')
        time.sleep(3)
        btncoment.click()




 bot = botComentario()
 bot.acessarPagina()

It returns the following error: Traceback (Most recent call last): File "botcomentador.py", line 60, in bot.accessPagina() File "botcomentador.py", line 49, in acessarPagina comment.send_keys(self.comment) File "C: Users Appdata Local Programs Python Python38-32 lib site-Packages Selenium webdriver remote webel self. _execute(Command.SEND_KEYS_TO_ELEMENT, File "C: Users Appdata Local Programs Python Python38-32 lib site-Packages Selenium webdriver remote webel , screen, stacktrace) Elementnotinteractableexception: Message: element not interactable (Session info: Chrome=81.0.4044.92)

1 answer

1


Hello, all right?

Your code has some errors and we will comment on them now.

The first crucial error in your code is to try to click and send the Keys, when you click the comment box the state changes and with that no longer possible where the Keys of the send Keys function.

The second error of the code is its scrollTo exaggerated by loading unnecessary objects on the page.

The third one that’s not really an error, is how you’re logging in, logging in directly to the youtube page on the most up-to-date browsers is not possible, so I’m going through another way to log in before going to the video itself, leaving your code more compatible to run elsewhere.

And finally using the time library Sleep is not very indicated since you can have different response times, depending on the speed of the internet and the processing for example. The right thing would be for you to check the DOM tree and compare URL’S in some cases, make it more performatic and decrease the chance of the code breaking. In the latter case I wouldn’t recommend but you can also make use of loops with Try and except.

Let’s get to the final code.

the code went like this:

import time
from selenium import webdriver


class botComentario:
    def __init__(self):        
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('lang=pt-br')
        self.driver = webdriver.Chrome(executable_path='g:\chromedriver.exe', chrome_options=chrome_options)
        self.username = "email"
        self.senha = "senha"
        self.comentario = "comentário"

    def logarConta(self):
        self.driver.get('https://accounts.google.com/signin/oauth/identifier?client_id=717762328687-iludtf96g1hinl76e4lc1b9a82g457nn.apps.googleusercontent.com&scope=profile%20email&redirect_uri=https%3A%2F%2Fstackauth.com%2Fauth%2Foauth2%2Fgoogle&state=%7B%22sid%22%3A526%2C%22st%22%3A%2259%3A3%3ABBC%2C16%3Ac3b339248cd15a98%2C10%3A1586882568%2C16%3A6523c4245b21192c%2C9be2dd9be623e3715fe47acbbc4fa7378cc9971848446578e27189dad062ea94%22%2C%22cdl%22%3Anull%2C%22cid%22%3A%22717762328687-iludtf96g1hinl76e4lc1b9a82g457nn.apps.googleusercontent.com%22%2C%22k%22%3A%22Google%22%2C%22ses%22%3A%227a0b075d423241f8bbcd97564752ce4b%22%7D&response_type=code&o2v=1&as=hX0j6XjfZGPt3PSNBNuS2A&flowName=GeneralOAuthFlow')
        time.sleep(3)
        logar = self.driver.find_element_by_xpath('//*[@id="identifierId"]')
        time.sleep(1)
        logar.click()
        logar.send_keys(self.username)
        conectar = self.driver.find_element_by_xpath('//*[@id="identifierNext"]/span')
        conectar.click()
        time.sleep(1)
        senha = self.driver.find_element_by_xpath('//*[@id="password"]/div[1]/div/div[1]/input')
        time.sleep(1)
        senha.click()
        senha.send_keys(self.senha)
        time.sleep(1)
        conectar = self.driver.find_element_by_xpath('//*[@id="passwordNext"]/span')
        conectar.click()

    def acessarPagina(self):
        time.sleep(5)
        self.driver.get('https://www.youtube.com/watch?v=R288k4p6ZkU')
        time.sleep(5)
        self.driver.execute_script("window.scrollTo(0, 500)")
        time.sleep(5)
        selecionarCampo = self.driver.find_element_by_id('placeholder-area')
        selecionarCampo.click()
        time.sleep(5)
        inputTexto = self.driver.find_element_by_id('contenteditable-root')
        inputTexto.send_keys(self.comentario)
        buttonEnviar = self.driver.find_element_by_xpath('//*[@id="submit-button"]')
        buttonEnviar.click()


bot = botComentario()
bot.logarConta()
bot.acessarPagina()
  • Thanks bro helped a lot, I’m new in Python and I like the language a lot, da para fazer muita coisa com ela.

  • Continue is an excellent language and good studies.

Browser other questions tagged

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