简体   繁体   English

替代 send_keys() - selenium python

[英]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.

相关问题 如何使用 selenium python 通过 send_keys 发送长文本 - how to send long text with send_keys using selenium python Python Selenium:输入文本框,send_keys 不起作用(angular javascript??) - Python Selenium: input textbox, send_keys not working (angular javascript??) Send_keys 长文本这么慢(Python + Selenium) - Send_keys long text so slowly (Python + Selenium) Selenium-send_keys()发送不完整的字符串 - Selenium - send_keys() sending incomplete string Python Selenium send_keys() 文本文件内空白区域的错误原因 - Python Selenium send_keys() error cause of the blank area inside the text file Send_keys函数确实按预期在Selenium python中工作 - Send_keys function does do its work as expected in Selenium python 用 javascript 执行脚本 python Z8E00596AD8DE22513FF8F8D8478 替换 xpath 找到的元素的 send_keys() - Replace send_keys() for an element found by xpath with javascript exec script python selenium 无法使用Selenium Webdriver中的send_keys上传文件 - Unable to Upload File using send_keys in Selenium Webdriver Selenium 3.141.0 - send_keys() 在 Windows 上工作正常,但在 Linux 上不行 Debian 11 - Selenium 3.141.0 - send_keys() work fine on Windows but don't on Linux Debian 11 在 selenium iframe 元素上触发 JavaScript 自定义事件,因为 send_keys 输入值不持久 - Trigger JavaScript custom event on selenium iframe element, as send_keys input values not persisting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM