发送密钥不起作用 selenium webdriver python
问题描述
我需要将文本发送到描述文本区域.有一些预定义的文本在点击后被清除.我尝试在 sendkeys 之前使用 clear() 或 click() ,但没有任何效果.它会在那里发送文本,但它仍然是灰色的,保存页面后出现描述中没有文本的错误...我可以使用其他东西代替发送密钥吗?谢谢
I need to send text to description textarea. There is some predefined text which is cleared after click. I tried to use clear() or click() before sendkeys but nothing works correctly. It will send text there but it is still grey and after saving page there is error that tere is no text in description... Can I use something else instead of send keys? Thanks
文本区域看起来像:
<textarea id="manage_description" class="eTextArea" name="e.description" cols="" rows="" onfocus="clearDescHint(this);" onblur="resetDescHint(this);" style="color: grey;"></textarea>
send_keys 不起作用
send_keys not working
self.driver.find_element_by_id('manage_description').send_keys("TEST")
解决方案
正如你提到的 send_keys("TEST")
不起作用,有几个替代方案将 字符序列
发送到如下所述的各个字段:
As you mentioned send_keys("TEST")
are not working, there are a couple of alternatives to send a character sequence
to respective fields as mentioned below :
使用
Keys.NUMPAD3
[模拟send_keys("3")
]:p>
login.send_keys(Keys.NUMPAD3)
将 JavascriptExecutor
与 getElementById
一起使用:
Use JavascriptExecutor
with getElementById
:
self.driver.execute_script("document.getElementById('login_email').value='12345'")
将 JavascriptExecutor
与 getElementsById
一起使用:
Use JavascriptExecutor
with getElementsById
:
self.driver.execute_script("document.getElementsById('login_password')[0].value='password'")
<小时>
现在谈到你的具体问题,正如你提到的 我尝试在 sendkeys 之前使用 clear() 或 click() 但没有任何效果
,所以我们将寻求 的帮助javascript
到click()
在文本区域清除预定义文本
然后使用send_keys
填充文本字段,如下所示:
Now comming to your specific issue, as you mentioned I tried to use clear() or click() before sendkeys but nothing works correctly
, so we will take help of javascript
to click()
on the text area to clear the predefined text
and then use send_keys
to populate the text field as follows:
self.driver.execute_script("document.getElementById('manage_description').click()")
self.driver.find_element_by_id('manage_description').send_keys("TEST")
更新:
正如您有时提到的,有时它会起作用,所以我建议如下:
Update :
As you mentioned sometimes it works sometimes not, so I would suggest the following:
- 诱导
ExplicitWait
使textarea
可点击. - 也可以使用
javascript
在textarea
内发送text
. 您的代码将如下所示:
- Induce
ExplicitWait
fortextarea
to be clickable. - Use
javascript
to send thetext
within thetextarea
too. Your code will look like:
my_string = "TEST";
elem = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "manage_description")))
self.driver.execute_script("document.getElementById('manage_description').click()")
self.driver.execute_script("arguments[0].setAttribute('value', '" + my_string +"')", elem);
相关文章