Skip to content

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 WebDriver and 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:

LocatorExampleNote
By.idBy.id("username")fastest, most stable — prefer it
By.cssSelectorBy.cssSelector(".btn.primary")flexible, readable
By.nameBy.name("q")form fields
By.xpathBy.xpath("//button[text()='OK']")powerful but brittle/slow — last resort
By.linkTextBy.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

  • WebDriver drives a real browser; ChromeDriver/etc. implement it, and Selenium Manager fetches the driver binary.
  • Always quit() (not just close()) to end the session and free the browser.
  • Find elements with locators; prefer By.id/data-testid over brittle XPath.
  • Use explicit waits (WebDriverWait + ExpectedConditions), never Thread.sleep.
  • Don't tune implicit and explicit waits high at the same time.

Lesson Quiz

Lesson Quiz · Selenium WebDriver Basics0 / 5
  1. What's the difference between driver.close() and driver.quit()?

    • AThey're identical
    • Bclose() closes the current window; quit() ends the whole session and frees the browser
    • Cquit() only minimizes
    • Dclose() ends the session
  2. Which locator is generally the most stable?

    • AA deep By.xpath mirroring the layout
    • BBy.id or a data-testid attribute
    • CBy.className on a styling class
    • DAbsolute XPath from html
  3. Why use an explicit wait instead of Thread.sleep?

    • AThread.sleep is illegal
    • BExplicit waits poll for a condition — fast when ready, and reliable when slow
    • CSleeps are faster
    • DExplicit waits never time out
  4. What does Selenium Manager (4.6+) do?

    • AWrites your tests
    • BAutomatically downloads the matching browser driver binary
    • CRuns the CI pipeline
    • DReplaces JUnit
  5. A recommended wait strategy is…

    • AHigh implicit AND high explicit waits together
    • BExplicit waits as primary, with implicit wait kept at 0 or small
    • COnly Thread.sleep
    • DNo waits at all

Next: Playwright for Java. This module's lab is in labs/src/main/java/com/jse21/m18_ui/.