Selenium with Python for Testing Login Pages
1. What is Selenium?
Selenium is an open-source automation tool used for testing web applications across different browsers. It allows you to simulate user actions like clicking buttons, entering text, or navigating between pages — all through code.
Selenium works with many programming languages, including Python, making it a popular choice for automated web testing.
2. Why Use Selenium for Login Testing?
Testing login functionality is critical because it:
Verifies access control
Confirms proper error handling (e.g., wrong password)
Ensures session management works correctly
Selenium helps automate and repeat these tests efficiently.
3. Basic Setup
a. Install Selenium
bash
Copy
Edit
pip install selenium
b. Download WebDriver
Each browser needs a corresponding driver:
Chrome → ChromeDriver
Firefox → GeckoDriver
Make sure the driver is in your system’s PATH or specify the path in code.
4. Sample Python Code for Login Test
python
Copy
Edit
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
# Set up the browser driver (Chrome example)
driver = webdriver.Chrome()
# Step 1: Open the login page
driver.get("https://example.com/login")
# Step 2: Locate username and password fields
username_input = driver.find_element(By.NAME, "username") # or By.ID, By.XPATH
password_input = driver.find_element(By.NAME, "password")
# Step 3: Enter credentials
username_input.send_keys("your_username")
password_input.send_keys("your_password")
# Step 4: Submit the form
login_button = driver.find_element(By.XPATH, "//button[@type='submit']")
login_button.click()
# Step 5: Wait and verify login
time.sleep(3)
# Check login success (e.g., check for dashboard or logout button)
if "dashboard" in driver.current_url:
print("Login successful!")
else:
print("Login failed!")
# Step 6: Close the browser
driver.quit()
5. Best Practices
Use waits: Replace time.sleep() with WebDriverWait for better reliability.
Handle invalid logins: Test scenarios with wrong credentials.
Use Page Object Model (POM): For cleaner, reusable test code.
Log and report: Record results of each test run.
6. Example: Using WebDriverWait
python
Copy
Edit
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# Wait until the element appears
wait = WebDriverWait(driver, 10)
dashboard_element = wait.until(EC.presence_of_element_located((By.ID, 'dashboard')))
7. Summary
Using Selenium with Python for testing login pages allows you to:
Automate repeated tests
Simulate real user interactions
Validate login success and error handling
Integrate with CI/CD pipelines for continuous testing
Learn Selenium Python Training in Hyderabad
Read More
Taking Screenshots with Selenium WebDriver in Python
How to Handle Multiple Browser Tabs or Windows in Selenium
Waits in Selenium: Implicit vs Explicit Waits Explained
Automating Form Filling Using Selenium and Python
Visit Our Quality Thought Training in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments