19

I am currently creating a selenium script in python. I need to enter something in a text box using send_keys function. It is doing that correctly as of now. However, for an observation for I need to slow down the speed at which send_keys populates the text field. Is there a way I can do that? Also, is there any alternate to send_keys in selenium? Thanks&Regards karan

4 Answers 4

30

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
Sign up to request clarification or add additional context in comments.

Comments

11

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

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)

Version with type hints:

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)

Usage:

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

Comments

0

Removing the space and slowing things down fixed the issue

def type_and_fill_slow(form_id,message):
    form_id.clear()
    for character in message:
        type(form_id)
        time.sleep(2)
        form_id.send_keys(character)    

Comments

0

This routine simulates clicking into the field and typing in a little more realistic fashion.

def simulate_typing(
    driver: WebDriver,
    element: WebElement,
    text: str,
    delay: float = 0.1,
):
    actions = ActionChains(driver, duration=20)
    actions.move_to_element(element)
    actions.click(element)
    actions.pause(delay)
    for letter in text:
        actions.send_keys(letter)
        actions.pause(delay)
    actions.perform()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.