Send keys not working selenium webdriver python(发送密钥不起作用 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
ExplicitWaitfortextareato be clickable. - Use
javascriptto send thetextwithin thetextareatoo. 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:发送密钥不起作用 selenium webdriver python
基础教程推荐
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
