Minh-Thanh Hoang
Minh-Thanh Hoang

Reputation: 556

InvalidElementStateException invalid element state: Element must be user-editable in order to clear it" error while sending text with Selenium Python

I have an input HTML element like this in Django

<input id="id" type="number" maxlength="50">

When I want to find and clear it

elm_input = self.wait.until(EC.presence_of_element_located((By.ID, elm_id)))
elm_input.clear()
elm_input.send_keys(value)

It's got error InvalidElementStateException

InvalidElementStateException invalid element state: Element must be user-editable in order to clear it"

We cannot send key clear because selenium know CLEAR or DELETE Keys is an Charactics Keys not an Number Keys, It's don't send Keys to element input. So how can I we fix it, I had try ActionChains but It's not working with as well

Upvotes: 2

Views: 5141

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193208

This error message...

InvalidElementStateException invalid element state: Element must be user-editable in order to clear it"

...implies that the WebDriver instance was unable to clear the existing contents of the element.


A bit more of the outerHTML of the element would have helped us to analyze the issue in a better way. However you need to take care of a couple of things as follows:

  • While sending a character sequence instead of using presence_of_element_located() you have to induce WebDriverWait for the element_to_be_clickable().
  • Ensure the Locator Strategy uniquely identifies the WebElement and you can use either of the following Locator Strategies:

    • Using CSS_SELECTOR:

      elm_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#id[type='number'][maxlength='50']")))
      elm_input.clear()
      elm_input.send_keys("1234567890")
      
    • Using XPATH:

      elm_input = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='id' and @type='number'][@maxlength='50']")))
      elm_input.clear()
      elm_input.send_keys("1234567890")       
      
    • 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
      

Reference

You can find a relevant discussion in:

Upvotes: 1

Related Questions