问题描述
这是有问题的HTML代码:
This is the HTML code in question:
<textarea rows="1" cols="1" name="text" class=""></textarea>
这是我的代码:
msgElem = driver.find_element_by_css_selector("textarea[name='text']")
driver.execute_script("arguments[0].click();", msgElem)
driver.execute_script("arguments[0].value = 'Whats up mate, how you doin';", msgElem)
msgElem.submit()
代码将执行,并且什么也没有发生.我假设它选择了textarea,但没有在其中输入任何内容?还是什么也没有发生.它还会找到该元素,因此我认为不需要等待textarea可见.当我不使用js而只是做
The code executes and nothing happens. I assume it selects the textarea but doesn't type nothing into it? or nothing happens at all. It also finds the element so I assume I don't need to wait for the textarea to be visible.When I don't use js and just do
msgElem = driver.find_element_by_css_selector("textarea[name='text']")
msgElem.send_keys('Whats up mate, how you doin')
它给了我ElementNotInteractableException.
It gives me ElementNotInteractableException.
推荐答案
此错误消息...
ElementNotInteractableException
...表示 WebDriver 实例无法与所需元素交互,因为.
...implies that the WebDriver instance was unable to interact with the desired element as the Element was Not Interactable.
您实际使用的定位器策略 HTML DOM 中的两个元素,而第一个匹配元素的父元素包含该属性 style ="display:none"
如下:
The Locator Strategy which you have used actually identifies two elements within the HTML DOM and the parent element of the first matching element contains the attribute style="display:none"
as follows:
<form action="#" class="usertext cloneable warn-on-unload" onsubmit="return post_form(this, 'comment')" style="display:none" id="form-dyo">
<input type="hidden" name="thing_id" value="">
<div class="usertext-edit md-container" style="">
<div class="md">
<textarea rows="1" cols="1" name="text" class=""></textarea>
</div>
因此,您会看到 ElementNotInteractableException .
要将字符序列发送到所需元素,您需要为 element_to_be_clickable()
引入 WebDriverWait ,并且您可以使用遵循定位器策略:
To send a character sequesce to the desired element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies:
-
使用
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.title + div textarea[name='text']"))).send_keys("Sowik")
使用 XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='message']//following::div[1]//textarea[@name='text']"))).send_keys("Sowik")
注意:您必须添加以下导入:
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
这篇关于Selenium不会键入到textarea并引发ElementNotInteractableException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!