How to Automate Login Using Selenium: Step-by-Step Guide
To automate login using
Selenium, first open the login page with driver.get(), then locate the username and password fields using reliable locators like By.ID or By.NAME. Use send_keys() to enter credentials and click() to submit the login button.Syntax
Here is the basic syntax to automate login with Selenium:
driver.get(url): Opens the login page URL.driver.find_element(By.LOCATOR, "value"): Finds an element on the page using a locator strategy like ID, NAME, or CSS_SELECTOR.element.send_keys("text"): Types text into input fields.element.click(): Clicks buttons or links.
python
from selenium import webdriver from selenium.webdriver.common.by import By # Open browser and navigate to login page driver = webdriver.Chrome() driver.get("https://example.com/login") # Find username and password fields username = driver.find_element(By.ID, "username") password = driver.find_element(By.ID, "password") # Enter credentials username.send_keys("myuser") password.send_keys("mypassword") # Click login button login_button = driver.find_element(By.ID, "loginBtn") login_button.click()
Example
This example demonstrates automating a login on a sample website by opening the page, entering username and password, and clicking the login button.
python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time # Setup Chrome options options = Options() options.add_argument('--headless') # Run browser in headless mode (no window) # Setup Chrome driver service service = Service() # Start WebDriver with webdriver.Chrome(service=service, options=options) as driver: driver.get("https://the-internet.herokuapp.com/login") # Locate username and password fields username = driver.find_element(By.ID, "username") password = driver.find_element(By.ID, "password") # Enter valid credentials username.send_keys("tomsmith") password.send_keys("SuperSecretPassword!") # Click login button login_button = driver.find_element(By.CSS_SELECTOR, "button.radius") login_button.click() # Wait for page to load time.sleep(2) # Check for success message success_message = driver.find_element(By.ID, "flash") print(success_message.text.strip())
Output
You logged into a secure area!\n×
Common Pitfalls
Common mistakes when automating login with Selenium include:
- Using unreliable locators like XPath with absolute paths that break easily.
- Not waiting for elements to load before interacting, causing errors.
- Hardcoding credentials in code instead of using secure storage.
- Ignoring page load or JavaScript delays after clicking login.
Always use stable locators like ID or NAME, and consider explicit waits.
python
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Wrong way: clicking immediately after sending keys login_button = driver.find_element(By.ID, "loginBtn") login_button.click() # May fail if button not ready # Right way: wait until button is clickable wait = WebDriverWait(driver, 10) login_button = wait.until(EC.element_to_be_clickable((By.ID, "loginBtn"))) login_button.click()
Quick Reference
| Step | Selenium Method | Description |
|---|---|---|
| Open page | driver.get(url) | Navigate to the login page URL |
| Find element | driver.find_element(By.ID, "value") | Locate input fields or buttons by ID |
| Enter text | element.send_keys("text") | Type username or password |
| Click button | element.click() | Submit the login form |
| Wait | WebDriverWait + expected_conditions | Wait for elements or page load |
Key Takeaways
Use stable locators like ID or NAME to find login elements reliably.
Always wait for elements to be ready before interacting to avoid errors.
Use send_keys() to enter username and password fields.
Click the login button to submit the form and trigger login.
Avoid hardcoding credentials; use secure methods for sensitive data.