InvalidElementStateException无效的元素状态:元素必须是用户可编辑的,以便清除它. Selenium Python发送文本时出错 [英] InvalidElementStateException invalid element state: Element must be user-editable in order to clear it" error while sending text with Selenium Python

查看:1340
本文介绍了InvalidElementStateException无效的元素状态:元素必须是用户可编辑的,以便清除它. Selenium Python发送文本时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有一个输入HTML元素

I have an input HTML element like this in Django

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

当我想查找并清除它时

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

出现错误InvalidElementStateException

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

我们无法发送键清除,因为硒知道CLEARDELETE键是一种功能键,而不是数字键,它不会将键发送到元素输入.因此,我们如何解决它,我尝试过ActionChains,但效果不佳

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

推荐答案

此错误消息...

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

...表示 WebDriver 实例无法清除元素的现有内容.

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

该元素的更多外部HTML可以帮助我们以更好的方式分析问题.但是,您需要注意以下几点:

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:

  • 在发送字符序列而不是使用presence_of_element_located()时,您必须为element_to_be_clickable()引入 WebDriverWait .
  • 确保定位器策略唯一地标识 WebElement ,并且您可以使用以下任一

  • 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:

  • 使用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")

  • 使用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
    

  • 您可以在以下位置找到相关讨论:

    You can find a relevant discussion in:

    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆