Appearance
Lesson 01 · Selenium WebDriver Basics
Beyond the 1Z0-830 exam
Selenium WebDriver is the long-standing standard for browser automation in Java: it drives a real browser through the W3C WebDriver protocol. Understanding its model — drivers, locators, and waits — is the foundation for any UI test, and for Playwright (next lesson) too.
Objectives
After this lesson you will be able to:
- Launch a browser with a
WebDriverand navigate to a page. - Find elements with locators (
By.id,By.cssSelector, …). - Interact with elements (
click,sendKeys, read text). - Use explicit waits instead of fixed sleeps, and explain implicit waits.
The WebDriver model
java
WebDriver driver = new ChromeDriver(); // Selenium Manager fetches the driver binary
try {
driver.get("https://example.com/login");
driver.findElement(By.id("username")).sendKeys("ada");
driver.findElement(By.id("password")).sendKeys("secret");
driver.findElement(By.cssSelector("button[type=submit]")).click();
assertEquals("Dashboard", driver.getTitle());
} finally {
driver.quit(); // always quit — closes the browser & session
}WebDriver is the interface; ChromeDriver, FirefoxDriver, EdgeDriver are implementations. Modern Selenium (4.6+) ships Selenium Manager, which downloads the matching driver binary automatically — no more manual chromedriver management.
Trap — quit(), not close()
driver.close() closes the current window; driver.quit() ends the whole session and frees the browser process. Leaking sessions (forgetting quit()) piles up zombie browsers in CI. Put it in a finally or an @AfterEach.
Locators
A locator (By) tells WebDriver how to find an element:
| Locator | Example | Note |
|---|---|---|
By.id | By.id("username") | fastest, most stable — prefer it |
By.cssSelector | By.cssSelector(".btn.primary") | flexible, readable |
By.name | By.name("q") | form fields |
By.xpath | By.xpath("//button[text()='OK']") | powerful but brittle/slow — last resort |
By.linkText | By.linkText("Sign out") | exact link text |
Prefer a stable hook
The most robust locator is one the app guarantees won't change for styling reasons — a data-testid attribute (By.cssSelector("[data-testid='submit']")). Locating by deep XPath or CSS that mirrors the visual layout breaks every time a designer touches the markup.
Waits — the heart of stability
The page is asynchronous; the element you want may not exist yet. There are three approaches, only two of which you should use:
java
// ❌ Fixed sleep — slow AND flaky. Never the answer.
Thread.sleep(3000);
// ✅ Explicit wait — poll until a specific condition holds (or time out).
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.id("submit")))
.click();
// Implicit wait — a global default applied to every findElement (set once).
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));- Explicit wait — waits for this condition on this element; the right tool for most cases.
- Implicit wait — a blanket "retry finds for up to N seconds"; convenient but coarse.
Gotcha — don't mix implicit and explicit waits
Combining implicit and explicit waits can compound timeouts unpredictably (e.g. a 5s implicit + 10s explicit interacting). Pick explicit waits as your primary strategy and keep implicit waits at 0 (or a small constant), not both tuned high.
Key Takeaways
WebDriverdrives a real browser;ChromeDriver/etc. implement it, and Selenium Manager fetches the driver binary.- Always
quit()(not justclose()) to end the session and free the browser. - Find elements with locators; prefer
By.id/data-testidover brittle XPath. - Use explicit waits (
WebDriverWait+ExpectedConditions), neverThread.sleep. - Don't tune implicit and explicit waits high at the same time.
Lesson Quiz
What's the difference between driver.close() and driver.quit()?
Which locator is generally the most stable?
Why use an explicit wait instead of Thread.sleep?
What does Selenium Manager (4.6+) do?
A recommended wait strategy is…
Next: Playwright for Java. This module's lab is in labs/src/main/java/com/jse21/m18_ui/.