Home > Software engineering >  ElementNotInteractableException in selenium using python
ElementNotInteractableException in selenium using python

Time:12-13

I'm trying to learn selenium, but i stumbled upon a error i can't seem to fix: ElementNotInteractableException (the code gives an timeoutexception). I have read various stackoverflow posts and tried the answers but none of them worked. I'm just trying to enter a few words in the search bar from Youtube. Anyway, here's the code.

import undetected_chromedriver.v2 as uc
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.keys import Keys

options = uc.ChromeOptions()
options.binary_location = "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
options.add_argument("--user-data-dir=c:\\temp\\testprofile2")
driver = uc.Chrome(options=options)
driver.get("https://www.youtube.com/")


def enter_search_term(driver):
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="search"]'))).send_keys("test")
    time.sleep(5)
    driver.quit()

enter_search_term(driver)

CodePudding user response:

The reason of the issue can be, that the element you try to send keys is not the real element which should recieve the input text.

1 Make sure that there is a single element found by '//*[@id="search"]'. (If there are multiple, your script will interact with the first found which may be hidden).

2 Make sure that the search field is not hidden, has positive width and height.

If you have some selenium record-play tool, like selenium IDE, you can try to perform all the steps manually with recording enabled, so you'll see the real element which recieve the search text in the output script.

Also try to look at this article enter image description here

then you can use the below XPath

//input[@id='search']

So your effective code will be:

options = uc.ChromeOptions()
options.binary_location = "C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"
options.add_argument("--user-data-dir=c:\\temp\\testprofile2")
driver = uc.Chrome(options=options)
driver.maximize_window()


driver.get("https://www.youtube.com/")


def enter_search_term(driver):
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='search']"))).send_keys("test")
    time.sleep(5)
    driver.quit()
enter_search_term(driver)

I have tested this and It works fine. Note that, I am maximizing the browser as well. and using visibility_of_element_located

  • Related