Write Your First Selenium Test Script
What We Will Automate
We will log in to a practice website made for testing: the-internet.herokuapp.com/login. Valid credentials are user tomsmith and password SuperSecretPassword!.
The Script
python
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.maximize_window()
try:
# 1. open the page
driver.get("https://the-internet.herokuapp.com/login")
# 2. find elements
username = driver.find_element(By.ID, "username")
password = driver.find_element(By.ID, "password")
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
# 3. act
username.send_keys("tomsmith")
password.send_keys("SuperSecretPassword!")
login_button.click()
# 4. read the result
message = driver.find_element(By.ID, "flash").text
print("Message:", message)
# 5. verify
assert "You logged into a secure area!" in message
print("TEST PASSED")
finally:
driver.quit() # always close the browser
Line by Line
| Code | Meaning |
|---|---|
webdriver.Chrome() | Start Chrome and return the driver object you use to control it |
driver.get(url) | Open a URL and wait until the page has loaded |
By.ID | Tells Selenium *how* to search (by id attribute) |
find_element(...) | Returns the first matching element or raises NoSuchElementException |
send_keys(text) | Types text into the element |
click() | Clicks the element |
.text | The visible text of the element |
assert | If the condition is false the test fails |
driver.quit() | Closes all windows and ends the driver process |
quit() vs close():
close() closes only the current window. quit() closes all windows and stops the driver. Always call quit() at the end, inside finally, so the browser closes even if the test fails.
Run It
bash
python first_test.py
Result
Message: You logged into a secure area! × TEST PASSED
Add Useful Extras
python
print(driver.title) # page title
print(driver.current_url) # current address
driver.save_screenshot("login.png")
driver.back() # browser Back button
driver.refresh() # reload
A Negative Test
Good testers also check what happens with wrong data.
python
driver.get("https://the-internet.herokuapp.com/login")
driver.find_element(By.ID, "username").send_keys("wrong")
driver.find_element(By.ID, "password").send_keys("wrong")
driver.find_element(By.CSS_SELECTOR, "button[type='submit']").click()
error = driver.find_element(By.ID, "flash").text
assert "Your username is invalid!" in error
Common mistakes: Forgetting
driver.quit() leaves browsers running. Searching an element before the page loaded causes errors (you will fix this with waits). Typos in the locator value give NoSuchElementException.
Slow it down while learning: Add
import time and time.sleep(2) after steps so you can watch. Remove these later and use proper waits.
Conclusion
A Selenium script follows four steps: open the page, find elements, act on them and verify the result. Always close the browser with quit() inside finally.
Quick Summary: get -> find_element -> send_keys/click -> read text -> assert -> quit.

