Message: element not interactable Python send keys

Viewed 19

I'm trying to send keys to the Youtube search box with Selenium, I've tried search ID and Xpath and consistently get errors. These errors include "Message: element not interactable", where am I going wrong?

from selenium import webdriver

from selenium.webdriver.common.keys import Keys

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
driver.get("https://youtube.com")

searchbox = driver.find_element("xpath", '//*[@id="search-input"]')
searchbox.send_keys('Jack West')
1 Answers

There are two problems:

First:

Your xpath gives <div> but you can't send keys to <div>

You have to get <input> which is inside <div>

'//*[@id="search-input"]//input'

And later it needs to click button to start searching.

Second:

When I run code browser displays popup window and asks to accept cookies, etc. And this popup blocks access to search field. It needs to add code which will accept it.


from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
#from webdriver_manager.firefox import GeckoDriverManager

import time
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
#driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()))

driver.get("https://youtube.com")
time.sleep(5)

print('cookies')
cookies = driver.find_elements(By.XPATH, '//div[contains(@class,"eom-button-row")]//a')
cookies[1].click()
time.sleep(5)

print('searchbox')
searchbox = driver.find_element(By.XPATH, '//*[@id="search-input"]//input')
searchbox.send_keys('Jack West')

print('searchbox button')
searchbox_button = driver.find_element(By.XPATH, '//button[@id="search-icon-legacy"]')
searchbox_button.click()

But it could be simpler to use url

https://www.youtube.com/results?search_query=Jack+West

and it doesn't need to click any keys or buttons.


YouTube has API for developers/programmers and this should be preferred method.

Related