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

帅比萌擦擦* 提交于 2020-01-15 04:41:27

问题


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


回答1:


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:

  • Invalid element state: Element must be user-editable in order to clear it error trying to click and insert a date on a dropdown-toggle using Selenium


来源:https://stackoverflow.com/questions/59572475/invalidelementstateexception-invalid-element-state-element-must-be-user-editabl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!