Search code examples
pythonselenium-webdriver

Selenium cannot send keys to youtube searchbox


Recently I started working a bit with selenium and encountered the problem that i could not send keys to the youtube searchbox with the following code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
service = Service(executable_path="chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://youtube.com")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "ytd-searchbox")))
input_element = driver.find_element (By.CLASS_NAME,"ytd-searchbox")
input_element.send_keys("some text"+Keys.ENTER)
time.sleep(100)

The name of the class should be correct because the searchbox gets highlighted when i run the function:

input_element.click()

The terminals output is:


DevTools listening on ws://127.0.0.1:61618/devtools/browser/a9e75182-8834-47ba-9b5d-94dad187f4d4
Traceback (most recent call last):
  File "c:\Users\noahm\Desktop\Vs_Code\programming\creativestuff\login_bot.py", line 13, in <module>
    input_element.send_keys("some text"+Keys.ENTER)
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webelement.py", line 231, in send_keys
    self._execute(
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\webdriver.py", line 354, in execute
    self.error_handler.check_response(response)
  File "C:\Users\noahm\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response  
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=126.0.6478.127)
Stacktrace:
        GetHandleVerifier [0x00007FF6B4ECEEA2+31554]
        (No symbol) [0x00007FF6B4E47ED9]
        (No symbol) [0x00007FF6B4D08559]
        (No symbol) [0x00007FF6B4D51D73]
        (No symbol) [0x00007FF6B4D4FEDB]
        (No symbol) [0x00007FF6B4D7D02A]
        (No symbol) [0x00007FF6B4D4BA76]
        (No symbol) [0x00007FF6B4D7D240]
        (No symbol) [0x00007FF6B4D9C977]
        (No symbol) [0x00007FF6B4D7CDD3]
        (No symbol) [0x00007FF6B4D4A33B]
        (No symbol) [0x00007FF6B4D4AED1]
        GetHandleVerifier [0x00007FF6B51D8B1D+3217341]
        GetHandleVerifier [0x00007FF6B5225AE3+3532675]
        GetHandleVerifier [0x00007FF6B521B0E0+3489152]
        GetHandleVerifier [0x00007FF6B4F7E776+750614]
        (No symbol) [0x00007FF6B4E5375F]
        (No symbol) [0x00007FF6B4E4EB14]
        (No symbol) [0x00007FF6B4E4ECA2]
        (No symbol) [0x00007FF6B4E3E16F]
        BaseThreadInitThunk [0x00007FFA88B1257D+29]
        RtlUserThreadStart [0x00007FFA8954AF28+40]



The same output emerged when i tried the code from this website: text

I would appreciate your help


Solution

  • It looks like there are at least 9 nodes that return with the selector ".ytd-searchbox".

    (using chrome inspector)
    > location.href
    'https://www.youtube.com/'
    > document.querySelectorAll(".ytd-searchbox")
    NodeList(9) [form#search-form.style-scope.ytd-searchbox, div#container.style-scope.ytd-searchbox, yt-icon#search-icon.style-scope.ytd-searchbox, input#search.ytd-searchbox, div#search-clear-button.style-scope.ytd-searchbox, ytd-button-renderer.style-scope.ytd-searchbox, button#search-icon-legacy.style-scope.ytd-searchbox, yt-icon.style-scope.ytd-searchbox, tp-yt-paper-tooltip.style-scope.ytd-searchbox]
    

    Generally it's not a good idea to match on the class attribute unless you have to.

    Oddly enough, in the case of YouTube's Search Box, "ID" isn't even unique 🤔

    Try matching on this CSS Selector instead:

    ...
    
    input_element = driver.find_element (By.CSS_SELECTOR, "input#search")
    input_element.send_keys("some text"+Keys.ENTER)
    
    ...