Selenium Python – Enter value in input text field
In this tutorial, you will learn how to enter a value in an input text field in Selenium Python.
To enter a value in an input text field in Selenium Python, you can use send_keys() method of the element object.
Steps to enter value in input text field
- Find the input text field element in the web page using
driver.find_element()
method. - Call
send_keys()
method on the input text field element, and pass the value that we want to enter in the input text field, as argument.
input_text_element.send_keys("Ram")
Example
In the following example, we initialize a Chrome webdriver, navigate to a specific URL that contains a form element with a couple of input text fields, take the screenshot, enter a value in the input text field whose id is "fname"
, and then take the screenshot again.
Python Program
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
# Setup chrome driver
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.set_window_size(500, 500)
# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')
# Find input text field
input_text_fname = driver.find_element(By.ID, 'fname')
# Take a screenshot before entering a value
driver.save_screenshot("screenshot-1.png")
# Enter a value in the input text field
input_text_fname.send_keys("Ram")
# Take a screenshot after entering a value
driver.save_screenshot("screenshot-2.png")
# Close the driver
driver.quit()
index.html
<html>
<body>
<h3>My Form</h3>
<form action="">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
screenshot-1.png

screenshot-2.png

Summary
In this Python Selenium tutorial, we have given instructions on how to enter a value in an input text field, using send_keys() method.