[英]Alternative to send_keys() - selenium python
I wanted to write faster alternative of this Python code:我想编写此 Python 代码的更快替代方案:
driver.get(myUrl)
message = driver.find_element_by_id('message')
send = driver.find_element_by_id('submit')
for _ in range(myRange):
message.send_keys(myText)
send.click()
As an alternative, tried to execute JavaScript with Selenium using execute_script()
and execute_async _script()
:作为替代方案,尝试使用execute_script()
和execute_async _script()
使用 Selenium 执行 JavaScript:
myJS = "document.getElementById('message').setAttribute('value', 'myText')"
for _ in range(myRange):
driver.execute_script(myJs)
send.click()
Both of them stopped after first loop cycle without any error.它们都在第一个循环循环后停止,没有任何错误。
What prevents the second script from looping through the whole range?是什么阻止了第二个脚本在整个范围内循环? And are there any other fast alternatives to send_keys()
?还有其他快速的send_keys()
替代方法吗?
Not sure the context of the test and why you are doing it, but this is how it should be done.不确定测试的上下文以及您为什么要这样做,但这是应该如何完成的。
driver.get(myUrl)
for _ in range(myRange):
# I would suggest using the explicit wait for the element staleness check here before
# entering the text
driver.find_element_by_id('message').send_keys(myText)
driver.find_element_by_id('submit').click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.