[英]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.