Iterate over Input Text Fields in Selenium Python

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:https://pythonexamples.org/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>
Copy

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('https://pythonexamples.org/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()
Copy

Screenshot

Python Selenium - Iterate over the input text fields and fill them with values

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('https://pythonexamples.org/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()
Copy

Screenshot

Python Selenium - Iterate over the input text fields and access their index

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('https://pythonexamples.org/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()
Copy

Screenshot

Python Selenium - Iterate over the input text fields and fill them with values from a list/array

Summary

In this Python Selenium tutorial, we learned how to iterate over the input text fields in the document, with the help of examples.

Related Tutorials

Quiz on Selenium

Q1. What is the method used to interact with web elements in Selenium Python?

Not answered

Q2. Which of the following is not a commonly used assertion method in Selenium Python?

Not answered

Q3. What is the method used to take a screenshot in Selenium Python?

Not answered

Q4. Which of the following is not a commonly used web element locator in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍