我目前正在python中创建一个selenium脚本。我需要使用send_keys函数在文本框中输入内容。到目前为止,它正在这样做。但是,为了观察我需要减慢send_keys填充文本字段的速度。有没有办法可以做到这一点?另外,selenium中有send_keys的替代品吗? 感谢和放大器;问候 卡兰
答案 0 :(得分:7)
您可以在发送每个字符后插入暂停。例如,如果您的代码如下所示:
el = driver.find_element_by_id("element-id")
el.send_keys("text to enter")
您可以将其替换为:
el = driver.find_element_by_id("element-id")
text = "text to enter"
for character in text:
el.send_keys(character)
time.sleep(0.3) # pause for 0.3 seconds
答案 1 :(得分:0)
遍历Mark的答案,您可以将其包装在可重用函数中:
(By.CLASS_NAME, 'card-details')
带有类型提示的版本:
import time
def slow_type(element, text, delay=0.1):
"""Send a text to an element one character at a time with a delay."""
for character in text:
element.send_keys(character)
time.sleep(delay)
用法:
import time
from selenium.webdriver.remote.webelement import WebElement
def slow_type(element: WebElement, text: str, delay: float=0.1):
"""Send a text to an element one character at a time with a delay."""
for character in text:
element.send_keys(character)
time.sleep(delay)