2

I try to use send_keys on a website, which gives me an error of element not interatable.

Here is my code below:

import selenium
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains
import time

chrome_optionsme = Options()
chrome_optionsme.add_argument("--incognito")
chrome_optionsme.add_argument("--window-size=1920x1080")
driver = webdriver.Chrome(options=chrome_optionsme, 
                          executable_path="/Users/chueckingmok/Desktop/html/chromedriver")

url='https://thelubricantoracle.castrol.com/industrial/en-DE'
driver.get(url)
time.sleep(5)

welcome_sec_one=WebDriverWait(driver,10).until(
    EC.presence_of_element_located((By.XPATH,'//*[@id="ctl00_termsPopup_lbConfirm"]'))
    )
welcome_sec_one.click()

time.sleep(5)



driver.find_element_by_xpath("//input[@class='search'[@id='txtSearch']").send_keys("Example")

![Uploaded the error image][1]

Here is the code 

ElementNotInteractableException           Traceback (most recent call last)
<ipython-input-2-1415cecd56f9> in <module>
----> 1 driver.find_element_by_xpath("//input[@class='search'][@id='txtSearch']").send_keys("Example")

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in send_keys(self, *value)
    477         self._execute(Command.SEND_KEYS_TO_ELEMENT,
    478                       {'text': "".join(keys_to_typing(value)),
--> 479                        'value': keys_to_typing(value)})
    480 
    481     # RenderedWebElement Items

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params)
    631             params = {}
    632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
    634 
    635     def find_element(self, by=By.ID, value=None):

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
    319         response = self.command_executor.execute(driver_command, params)
    320         if response:
--> 321             self.error_handler.check_response(response)
    322             response['value'] = self._unwrap_value(
    323                 response.get('value', None))

/Applications/anaconda3/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
    240                 alert_text = value['alert'].get('text')
    241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
    243 
    244     def _value_or_default(self, obj, key, default):

ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=83.0.4103.116)

I think the problem is that I cannot locate the element eventually. So, that's why the element not interactable. btw, the website link: https://thelubricantoracle.castrol.com/industrial/en-DE# I want to use the search button.

Anyone can help?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Joan Mok
  • 91
  • 3
  • 9

1 Answers1

3

This error message...

ElementNotInteractableException: Message: element not interactable

...implies that the WebElement with whom you are trying to interact isn't interactable (isn't in interactable state) at that point of time.

The two(2) main reasons for this error are:

  • Either you are trying to interact with a wrong/mistaken element.
  • Or you are invoking click() too early even before the element turns clickable / interactable.

To send a character sequence with in the search field you you have to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:

Code Block:

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

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://thelubricantoracle.castrol.com/industrial/en-DE")
WebDriverWait(driver, 20).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')
time.sleep(3)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='button primary' and contains(@id, 'termsPopup_lbConfirm')]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='search-init']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='search']"))).send_keys("Example")

Browser Snapshot:

castrol


Reference

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    Many thanks!!! May I ask about the xpath of "//div[@id='search-init']" and "//input[@class='search']" ? As I inspect it, the xpath is '//*[@id="txtSearch"]' this. Why your xpath is different? – Joan Mok Jul 09 '20 at 21:54
  • @JoanMok The website is Javascript based. Initially the search field is `//div[@id='search-init']` but as soon as you click within the field, visually the control is transferred to the `//input[@class='search']` where you need to pass the search text. – undetected Selenium Jul 09 '20 at 21:58
  • So, I should learn the Javascript to inspect this website? – Joan Mok Jul 09 '20 at 22:56
  • @JoanMok Not at all, you shouldn't be worried about the technology involved in building the applications. But while you learn [Selenium](https://stackoverflow.com/questions/54459701/what-is-selenium-and-what-is-webdriver/54482491#54482491) to interact with the [WebElement](https://stackoverflow.com/questions/52782684/what-is-the-difference-between-webdriver-and-webelement-in-selenium/52805139#52805139) within the HTML, gradually you will learn the Javascript and other relevant stuffs at the same time. **Note**: I haven't mentioned about JS till you asked for a clarification :) Happy learning – undetected Selenium Jul 09 '20 at 23:01
  • How can I detect the website is Javascript based or HTML? What about the meaning of this code? 'WebDriverWait(driver, 20).until(lambda driver: driver.execute_script('return document.readyState') == 'complete')' It has the' execute_script' code. – Joan Mok Jul 09 '20 at 23:05
  • These days the [DOM Tree](https://javascript.info/dom-nodes) rendered are rarely static. Modern websites uses [JavaScript](https://www.javascript.com/), [Angular](https://angular.io/), [ReactJS](https://reactjs.org/), [Vue.js](https://vuejs.org/), [Ember.js](https://www.emberjs.com/), [jQuery](https://jquery.com/), [AJAX](https://www.w3schools.com/xml/ajax_intro.asp), etc to render the [HTML DOM](https://www.w3schools.com/js/js_htmldom.asp). But you don't need to worry about the underlying technology stack right away and remain focused on how to deal with them using Selenium. – undetected Selenium Jul 09 '20 at 23:10
  • Could you show me the whole Javascript of this xpath, please? – Joan Mok Jul 09 '20 at 23:11
  • That itself is a bigger question. However it will be unjustified to push you towards Javascript right away, you being a beginner with Selenium. However if you have a specific question feel free to raise it. – undetected Selenium Jul 09 '20 at 23:14
  • I have also found that some tip tip message that only cover a small part of the html elements may cause this error. – Sergio Prats Nov 11 '20 at 11:19