send_keys() method in Selenium Python

The send_keys() method in Selenium Python is a useful tool that can be used to automate typing into an element. This method is particularly helpful when interacting with web pages that require user input, like filling out forms or entering text into input fields. In this post, we will take a detailed look at the send_keys() method and its functionality.

  • Method declaration – def send_keys(self, *value) -> None
  • What does it do? The send_keys() method simulates keyboard typing into an element. We can pass a *value as an argument to this method, which allows us to send a list, tuple, or multiple string arguments. The method then types the contents of these arguments into the element.
Typing into an input field

Let’s take an example of the below-highlighted element present on the page – https://testkru.com/Elements/TextFields where we will try to type “codekru”.

text element

We can find the element with the help of the find_element() method using its id.

element = driver.find_element(By.ID, 'firstName')

Then, we can type “codekru” using the send_keys() method.

element.send_keys("codekru")

Whole Code

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/TextFields")

element = driver.find_element(By.ID, 'firstName')
element.send_keys("codekru")

options.add_experimental_option("detach", True) is used so the browser doesn’t close automatically after the execution is completed.

The above code will type “codekru” in the required field, as shown below.

text field filled using send_keys
Passing multiple keywords in the input field

We can pass multiple strings in the send_keys() method, and all passed keywords will be typed in the input field.

element.send_keys("codekru","tutorials","for","selenium","python")

Whole code

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/TextFields")

element = driver.find_element(By.ID, 'firstName')
element.send_keys("codekru","tutorials","for","selenium","python")
Entering multiple strings
Passing list in the send_keys() method

We can pass a list to the send_keys() method, and its contents will be typed into the element.

list = ["codekru","tutorials","for","selenium","python"]
element = driver.find_element(By.ID, 'firstName')
element.send_keys(list)

Whole code

from selenium import webdriver
from selenium.webdriver.common.by import By

options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
driver = webdriver.Chrome(options)
driver.get("https://testkru.com/Elements/TextFields")

list = ["codekru","tutorials","for","selenium","python"]
element = driver.find_element(By.ID, 'firstName')
element.send_keys(list)

This will also enter the keywords in the element.

We hope that you like the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *