Last modified: Dec 02, 2023 By Alexander Williams
Python Selenium: Fill Input Fields - Examples
Example 1: Fill Text Input Field
from selenium import webdriver
# Create a Chrome driver
driver = webdriver.Chrome()
# Navigate to a webpage with a text input field
driver.get('https://example.com')
# Find the text input field by its ID
text_input = driver.find_element_by_id('text-input-id')
# Input text into the text input field
text_input.send_keys('Example Text')
Output:
# The text input field on the webpage is filled with the text "Example Text".
Example 2: Fill Password Input Field
# Continue from the previous example
# Find the password input field by its name attribute
password_input = driver.find_element_by_name('password')
# Input a password into the password input field
password_input.send_keys('SecurePassword123')
Output:
# The password input field on the webpage is filled with the password "SecurePassword123".
Example 3: Fill Email Input Field with Clearing
# Continue from the previous example
# Find the email input field by its class name
email_input = driver.find_element_by_class_name('email-input-class')
# Clear the existing content in the email input field
email_input.clear()
# Input an email address into the cleared email input field
email_input.send_keys('user@example.com')
Output:
# The email input field on the webpage is cleared, and the email address "user@example.com" is input.
Example 4: Fill Multiple Input Fields Simultaneously
# Continue from the previous example
# Find the input fields by their CSS selector
input_fields = driver.find_elements_by_css_selector('.input-field-class')
# Input data into multiple input fields simultaneously
input_data = ['Value1', 'Value2', 'Value3']
for field, value in zip(input_fields, input_data):
field.send_keys(value)
# Close the browser
driver.quit()
Output:
# Multiple input fields on the webpage are filled simultaneously with the values "Value1", "Value2", and "Value3".