Software Testing Selenium Basics
Selenium is a free, open-source toolkit that controls a web browser through code. A Selenium script opens a page, clicks buttons, types text, and reads results exactly as a person would, only faster. Testers use it to automate repeated browser checks, such as login and checkout flows.
Selenium Components
| Component | Purpose |
|---|---|
| Selenium WebDriver | Core library that sends commands to browsers |
| Selenium IDE | Browser extension that records and replays simple tests |
| Selenium Grid | Runs tests on many browsers and machines in parallel |
How WebDriver Works
Test Script ---> WebDriver API ---> Browser Driver ---> Real Browser
(your code) (chromedriver, (Chrome,
geckodriver) Firefox)
<---------------------- results come back -------------------
The script sends a command, the driver translates it for the browser, and the browser performs the action. Recent Selenium versions download matching drivers automatically.
Supported Languages
Selenium offers bindings for Java, Python, C#, JavaScript, and Ruby. Beginners often choose Python for its short, readable code.
Setup Steps
- Install Python and a code editor.
- Install the library with the command
pip install selenium. - Install a browser such as Chrome.
- Write a short script and run it.
First Script
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://example.com/login")
driver.find_element(By.ID, "username").send_keys("demo_user")
driver.find_element(By.ID, "password").send_keys("demo_pass")
driver.find_element(By.ID, "loginBtn").click()
title = driver.title
print("PASS" if "Dashboard" in title else "FAIL")
driver.quit()
Understanding Each Line
webdriver.Chrome()opens a new Chrome window.driver.get()loads the address.find_elementlocates one item on the page.send_keystypes text into a field.clickpresses a button.driver.quit()closes the browser and frees resources.
Locators
A locator tells Selenium which element to use. Stable locators make tests reliable.
| Locator | Example | Reliability |
|---|---|---|
| ID | By.ID, "email" | Best when unique |
| Name | By.NAME, "q" | Good |
| CSS Selector | By.CSS_SELECTOR, ".btn-primary" | Flexible and fast |
| XPath | By.XPATH, "//button[text()='Save']" | Powerful, can be fragile |
| Link Text | By.LINK_TEXT, "Sign in" | Easy for links |
Waiting for Elements
Pages load at different speeds. A script that clicks before a button appears fails. Explicit waits pause until a condition becomes true.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
button = wait.until(EC.element_to_be_clickable((By.ID, "loginBtn")))
button.click()
Avoid fixed sleep commands. They slow tests and still fail on slow days.
Test Structure Diagram
Setup (open browser)
|
Actions (click, type)
|
Assertions (check result)
|
Teardown (close browser)
Page Object Model
The Page Object Model places the locators and actions of each page inside one class. Tests then call simple methods such as login_page.login(user, pass). When the page changes, the team fixes one class instead of fifty tests.
Limits
- Selenium automates web browsers only, not desktop or mobile native apps.
- Selenium has no built-in reporting, so teams combine it with test frameworks such as pytest or TestNG.
- UI tests run slower than API and unit tests.
Best Practices
- Choose IDs and data attributes over long XPath expressions.
- Keep every test independent.
- Run tests in headless mode inside the build pipeline.
- Capture a screenshot when a test fails.
Running Tests as a Suite
Wrap scripts inside a test framework such as pytest. Each test becomes a function with a clear name, and the framework runs all of them, collects results, and prints a summary. Add a fixture that opens the browser before each test and closes it afterward. Run the suite from the command line or from a build pipeline so every code change triggers the same checks automatically.
