简体   繁体   English

如何在Python中使用Selenium更改输入元素中的值?

[英]How to change a value in an input element using Selenium in Python?

I got a input element that I need to clear the actual value and input another one. 我得到了一个输入元素,需要清除实际值并输入另一个。

The HTML has the following structure: HTML具有以下结构:

<input class="input-mini" type="text" name="daterangepicker_start" value="">

I used the following code to find this element: 我使用以下代码查找此元素:

test = browser.find_elements_by_css_selector("input[type='text'][name='daterangepicker_start']")

Then I got a list of 7 elements and I guess the first one is the one. 然后我得到了7个元素的列表,我想第一个元素就是一个。 So a tried to clear the field and after send the new value as following: 因此,尝试清除该字段并在发送新值之后如下:

test[0].clear()
test[0].send_keys('02/07/2019')

However, for both codes I got the same error: 但是,对于这两个代码,我得到了相同的错误:

ElementNotInteractableException: element not interactable
  (Session info: chrome=76.0.3809.87)

Use WebDriverWait 使用WebDriverWait

test = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR , "input[type='text'][name='daterangepicker_start']")))
test[0].clear()
test[0].send_keys('02/07/2019')

Sometimes, the element can be under a <div that is considered hidden , I have experienced this before, sometimes you can try .click() to make the element Interactable , like this: 有时,该元素可以位于被认为是隐藏<div下,我之前已经经历过,有时您可以尝试.click()使该元素可交互 ,如下所示:

test[0].click()
test[0].clear()
test[0].send_keys('02/07/2019')

In case the above does not allow the element to change to an interactable state, try changing the value with javascript: 如果以上不允许元素更改为可交互状态,请尝试使用javascript更改值:

browser.execute_script("arguments[0].value = arguments[1]", test[0], "02/07/2019")

I would also recommend to look through the element list and figure out which one within the list is actually the element you're trying to interact with, in case it's not truly test[0] . 我还建议您浏览一下元素列表,弄清楚列表中的哪个元素实际上是您要与之交互的元素,以防万一它不是真正的test[0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium 和 Python 单击值为“下载目录”的输入元素 - How to click on the input element with value as Download Catalogues using Selenium and Python 如何使用python(selenium)更改网页元素的值 - How to change the value of a web page element using python (selenium) 如何使用 Python 和 Selenium 更改元素的 class 属性的值 - How to change the value of the class attribute of an element using Python and Selenium Python - 如何使用 Selenium 更改输入字段值 - Python - How to change the input field value using Selenium for 如何使用 Python 更改隐藏输入值 Selenium - How do I change a hidden input value with Selenium using Python 如何使用 Selenium 和 Python 单击输入元素 - How to click on an input element using Selenium and Python 使用for循环Selenium Python将随机值发送到输入元素 - Send a random value to an input element using for loop Selenium Python 如何使用 Selenium 和 Python 更改日期选择器的值 - How to change the value of datepicker using Selenium and Python 无法使用 Selenium 和 Python 更改角度元素文本/值 - Unable to change angular element text/value using Selenium and Python 在python中使用selenium更改html输入标签中的值字段 - Change value field in html input tag using selenium in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM