I was trying to make a program that sends comment under a youtube's video while a send_keys() problem suddenly appeard

Viewed 93

I have written this code, for send comments under a youtube video, but when I execute it, it gaves me this error (line 20, the line of msg_box.send_keys(msg)):

Traceback (most recent call last):
  File "C:/Users/isiac/PycharmProjects/webtest/youtube test.py", line 20, in <module>
    msg_box.send_keys(msg)
  File "C:\Users\isiac\PycharmProjects\webtest\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 477, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
  File "C:\Users\isiac\PycharmProjects\webtest\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\isiac\PycharmProjects\webtest\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\isiac\PycharmProjects\webtest\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

idk why, can you help me?

from selenium import webdriver
import time

again = str("yes")

while again == 'yes':
    driver = webdriver.Chrome()
    driver.get("https://www.youtube.com/watch?v=youtubevideo")
    driver.maximize_window()

    msg = input("write your message  ")
    count = int(input("send how much time you want to send it  "))

    input("ready and waiting  ")

    msg_box = driver.find_element_by_xpath("/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[5]/ytd-comment-simplebox-renderer/div[1]/yt-formatted-string")

    for index in range(count):
        time.sleep(5)
        msg_box.send_keys(msg)
        time.sleep(5)
        driver.find_element_by_xpath("/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[5]/ytd-comment-simplebox-renderer/div[3]/ytd-comment-dialog-renderer/ytd-commentbox/div/div[4]/div[5]/ytd-button-renderer[2]/a/paper-button").click

    again = str(input("do you want to do it again?  "))

    print("acknowledge")
1 Answers

I ran your code on my machine line by line, and the problem is that when the page is first loaded, the msg_box element is not loaded, so your first XPath query fails. The comment element doesn't load until the page is scrolled down. This behavior may be dependent on screen size, but I'm on a laptop and don't have a large monitor to test.

In order to scroll down so the comment box is enabled, modify your script like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys # needed to send END key
import time

again = "yes" # "yes is already a string

while again == "yes":
    driver = webdriver.Chrome()
    driver.get("https://www.youtube.com/watch?v=youtubevideo")
    driver.maximize_window()
    time.sleep(1)
    driver.find_element_by_tag_name('body').send_keys(Keys.END) # this is the important part

    msg = input("write your message  ")
    count = int(input("send how much time you want to send it  "))

    print("ready and waiting") # we don't need input() here

    msg_box = driver.find_element_by_xpath("/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[5]/ytd-comment-simplebox-renderer/div[1]/yt-formatted-string")

    for index in range(count):
        time.sleep(5)
        msg_box.send_keys(msg)
        time.sleep(5)
        driver.find_element_by_xpath("/html/body/ytd-app/div/ytd-page-manager/ytd-watch-flexy/div[4]/div[1]/div/ytd-comments/ytd-item-section-renderer/div[1]/ytd-comments-header-renderer/div[5]/ytd-comment-simplebox-renderer/div[3]/ytd-comment-dialog-renderer/ytd-commentbox/div/div[4]/div[5]/ytd-button-renderer[2]/a/paper-button").click()

    again = input("do you want to do it again?  ") # input() always returns a str

    print("acknowledge")

I didn't actually sign in to YouTube and leave a comment on a random video, so I don't know if your second XPath is valid, but hopefully you get the idea.

Related