Iterate over Input Text Fields in Selenium
Selenium - Iterate over input text fields
To iterate over all the input text fields in Selenium Python, you can call the find_elements() method on the driver object and find all the input text fields in the document, then use a For loop to iterate over these found input text fields.
In this tutorial, you will learn how to find all the input text fields, and then iterate over them using a For loop.
Examples
In the following examples, we shall use the below webpage at URL:/tmp/selenium/index-39.html. It has three input text fields.
<html>
<body>
<h1>My Sample Form</h1>
<form action="" id="myform">
<label for="firstname">First name:</label>
<input type="text" id="myfirstname" name="firstname"><br><br>
<label for="lastname">Last name:</label>
<input type="text" id="mylastname" name="lastname"><br><br>
<label for="age">Age:</label>
<input type="text" id="myage" name="age"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
1. Iterate over the input text fields and fill them with values
In the following program, we initialize a Chrome webdriver, load URL for a webpage with a form element that has three input text fields. We shall find all input text fields, iterate over them and set their value with the text "Apple"
.
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
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('/tmp/selenium/index-39.html')
# Find all input text fields
input_text_fields = driver.find_elements(By.XPATH, '//input[@type="text"]')
# Iterate over the input text fields
for input_text in input_text_fields:
input_text.send_keys("Apple")
# Take a screenshot
driver.save_screenshot("screenshot.png")
# Close the driver
driver.quit()
Screenshot
2. Iterate over the input text fields and access their index
We can use For loop with enumerate function, to access the index of respective input text field inside the loop.
In the following program, we iterate over the input text fields, access their index, and enter a value in them whose value is a string "Apple "
followed by their index.
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
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('/tmp/selenium/index-39.html')
# Find all input text fields
input_text_fields = driver.find_elements(By.XPATH, '//input[@type="text"]')
# Iterate over the input text fields
for index, input_text in enumerate(input_text_fields):
input_text.send_keys("Apple " + str(index))
# Take a screenshot
driver.save_screenshot("screenshot.png")
# Close the driver
driver.quit()
Screenshot
3. Iterate over the input text fields and fill them with values from a list/array
While iterating over the input text fields, we can enter values in them taken from a Python List.
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
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))
driver.set_window_size(500, 400)
# Navigate to the url
driver.get('/tmp/selenium/index-39.html')
# Find all input text fields
input_text_fields = driver.find_elements(By.XPATH, '//input[@type="text"]')
# List of values to be filled in the text boxes
mylist = ['Apple', 'Banana', 25]
# Iterate over the input text fields
for index, input_text in enumerate(input_text_fields):
input_text.send_keys(mylist[index])
# Take a screenshot
driver.save_screenshot("screenshot.png")
# Close the driver
driver.quit()
Screenshot
Summary
In this Python Selenium tutorial, we learned how to iterate over the input text fields in the document, with the help of examples.