发送密钥不起作用Selenium WebDriver Python [英] Send keys not working 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")
无法正常工作,有两种方法可以将character sequence
发送到各个字段,如下所示:如下所述:
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")
]:
Use
Keys.NUMPAD3
[simulatingsend_keys("3")
]:
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'")
现在就开始提到您所提到的特定问题,就像您提到的I tried to use clear() or click() before sendkeys but nothing works correctly
一样,因此我们将在文本区域上将 javascript
转换为 click()
,以进行清除predefined text
,然后使用 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);
这篇关于发送密钥不起作用Selenium WebDriver Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!