Complete Guide to Auto-filling Username and Password Using Selenium in Python

Nov 30, 2025 · Programming · 10 views · 7.8

Keywords: Selenium | Python | Web Automation | Username Filling | Password Authentication

Abstract: This article provides a comprehensive guide on automating username and password filling in login forms using Selenium WebDriver in Python. It covers the new API in Selenium 4.3.0+, element locating strategies, form submission techniques, and common troubleshooting. With complete code examples and step-by-step explanations, it helps developers master authentication flow implementation in web automation testing.

Introduction to Selenium WebDriver

Selenium is a powerful web automation testing framework widely used for functional testing, regression testing, and web application automation. The Python language, through the selenium package, offers concise API interfaces, making web automation script writing efficient and convenient.

Core Changes in New Selenium API

Starting from Selenium version 4.3.0 (released in June 2022), the original find_element_by_* and find_elements_by_* methods have been removed, replaced by the unified find_element() method with the By enum class. This change enhances code consistency and maintainability.

Implementation Steps for Username and Password Filling

Below is the complete process for auto-filling username and password using the new API:

First, import the necessary modules:

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

Initialize the WebDriver and open the target webpage:

driver = webdriver.Chrome()  # or Firefox(), Edge(), etc.
driver.get('http://www.example.com/login')

Locate the username input field and fill in the data:

# Locate element by ID
username_field = driver.find_element(By.ID, "username")
username_field.send_keys("your_username")

# Alternative by NAME locator
# username_field = driver.find_element(By.NAME, "username")
# username_field.send_keys("your_username")

Locate the password input field and fill in the data:

password_field = driver.find_element(By.ID, "password")
password_field.send_keys("your_password")

Form Submission Methods

After filling in the authentication information, the form can be submitted in several ways:

Method 1: Click the submit button

submit_button = driver.find_element(By.NAME, "submit")
submit_button.click()

Method 2: Simulate the Enter key

password_field.send_keys(Keys.RETURN)

Method 3: Direct form submission

form = driver.find_element(By.ID, "loginForm")
form.submit()

Common Errors and Considerations

A common mistake when using the Select class: Select() is specifically designed for HTML <select> elements (dropdowns), not ordinary text input fields. For username and password text inputs, use the send_keys() method directly.

Element locating accuracy: Ensure the locators (ID, NAME, CLASS_NAME, etc.) exactly match the actual element attributes on the page, including case sensitivity.

Complete Example Code

Here is a full example of login automation:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

# Initialize browser driver
driver = webdriver.Chrome()

try:
    # Open login page
    driver.get('https://example.com/login')
    
    # Fill username
    username = driver.find_element(By.ID, "username")
    username.clear()  # Clear any default values
    username.send_keys("test_user")
    
    # Fill password
    password = driver.find_element(By.ID, "password")
    password.send_keys("secure_password123")
    
    # Submit login form
    login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
    login_button.click()
    
    # Wait for page redirect
    time.sleep(2)
    
    # Verify login success
    if "dashboard" in driver.current_url:
        print("Login successful!")
    else:
        print("Login failed")
        
finally:
    # Close browser
    driver.quit()

Best Practices Recommendations

In real-world projects, it is recommended to adopt the following best practices: Use explicit waits instead of fixed-time sleep to improve script stability and efficiency; manage sensitive information like passwords using environment variables or configuration files; add exception handling to ensure script robustness; regularly update Selenium versions to access the latest features and security fixes.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.