ElementNotInteractableException: Message: element not interactable error while clicking on youtube search button using Selenium and ChromeDriver

Viewed 655

I'm using latest python and latest Selenium chrome webdriver. I'm trying to have a simple code to search in youtube but I'm getting the below error. Can anyone help me?

File "search.py", line 8, in <module> searchBox.click()
Selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

CodeStartsHere:

from selenium import webdriver

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

searchBox = driver.find_element_by_xpath('//*[@id="search"]')
if searchBox.is_enabled():
    searchBox.click()
    searchBox.send_keys("youtube test")
    searchButton = driver.find_element_by_xpath('//*[@id="search-icon-legacy"]/yt-icon')
    searchButton.click()
else:
    print("What the heck")
#CodeEndsHere
2 Answers

To initiate a search within https://youtube.com you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://youtube.com')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='search_query']"))).send_keys("youtube test")
    driver.find_element_by_css_selector("button#search-icon-legacy>yt-icon").click()
    
  • Using XPATH:

    driver.get('https://youtube.com')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='search_query']"))).send_keys("youtube test")
    driver.find_element_by_xpath("//button[@id='search-icon-legacy']/yt-icon").click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

Browser Snapshot:

youtube_search


Reference

You can find a couple of relevant discussions in:

I found 3 elements with your tag on Youtube homepage, the 1st one is invisible, the 2nd one is the search field which you are trying to reach. Use this tag:

"//div[@id='search-input']"
Related