user13341805
user13341805

Reputation: 13

Selenium finds wrong element, an element that doesn't even exist

I'm inspecting the youtube search bar in firefox and get:

<input id="search" autocapitalize="none" autocomplete="off" autocorrect="off" name="search_query" tabindex="0" type="text" spellcheck="false" placeholder="Search" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" dir="ltr" style="outline: currentcolor none medium;" class="ytd-searchbox">

I have the following code:

driver = webdriver.Firefox()
driver.get("http://www.youtube.com")
elem = driver.find_element_by_id("search")
elem.send_keys("asdfasdf")
elem.send_keys(Keys.RETURN)

and get the following error (using firefox gecko webdriver):

    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.ElementNotInteractableException: Message: Element <g id="search"> is not reachable by keyboard

Why? When I inspect the source code there is no g element with id="search" so what is going on?

Upvotes: 1

Views: 225

Answers (1)

SeleniumUser
SeleniumUser

Reputation: 4177

ElementNotInteractableException is occured when an element is found, but you can not interact with it.

There are so many reasons of it:

  • element is not visible / not displayed
  • element is off screen
  • element is behind another element or hidden

To resolve your issue you need to use actionchain, Refer below solution:

Try below code:

url = 'http://www.youtube.com'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)

element = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@id='search-form']//div[@id='container']//div[@id='search-input']//input")))

actionChains = ActionChains(driver)
actionChains.move_to_element(element).click().perform()
actionChains.move_to_element(element).send_keys("Bollywood",Keys.RETURN).perform()

Note : please add below imports to your solution

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

Output:

enter image description here

Upvotes: 1

Related Questions