2

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
codehunt
  • 43
  • 4

2 Answers2

1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

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']"
Villa_7
  • 508
  • 4
  • 14
  • Thank you for quick help, changed code as per your suggestion: searchBox = driver.find_element_by_xpath('//div[@id="search-input"]') But still hit with this error. File "search.py", line 9, in searchBox.send_keys("youtube test") selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable – codehunt Jun 06 '20 at 17:42