How to slow down the speed of send_keys action in a selenium script using python? How to slow down the speed of send_keys action in a selenium script using python? selenium selenium

How to slow down the speed of send_keys action in a selenium script using python?


You could insert a pause after each character is sent. For example, if your code looked like this:

el = driver.find_element_by_id("element-id")el.send_keys("text to enter")

You could replace it with:

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


Iterating over Mark's answer, you can wrap it up in a reusable function:

import timedef 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)

Version with type hints:

import timefrom selenium.webdriver.remote.webelement import WebElementdef 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)

Usage:

el = driver.find_element_by_id("element-id")text = "text to enter"slow_type(el, text)