Your First Selenium Script
With the Maven project in place, let's write a complete, real script: open a browser, navigate to the RecipeBox login page, type an email address into the email field, and close the browser cleanly. Every line here is something you'll use in nearly every Selenium test you ever write.
The Complete Script
package com.recipebox.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
try {
driver.get("https://recipebox.example.com/login");
driver.findElement(By.id("email")).sendKeys("tester@example.com");
System.out.println("Page title: " + driver.getTitle());
} finally {
driver.quit();
}
}
}
main method for learning purposes. Once we
introduce TestNG in a later lesson, this same logic will move into a proper @Test method
— the WebDriver calls themselves don't change at all.Line by Line
| Line | What it does |
|---|---|
import org.openqa.selenium.WebDriver; | Brings in the core WebDriver interface — every Selenium script needs this. |
import org.openqa.selenium.chrome.ChromeDriver; | Brings in the Chrome-specific implementation of WebDriver. |
WebDriver driver = new ChromeDriver(); | Launches a real Chrome browser window and hands you a reference to control it. Under the hood, Selenium Manager finds/downloads a matching chromedriver automatically. |
driver.get("https://recipebox.example.com/login") | Navigates the browser to the given URL and waits for the page to finish loading before returning control to your script. |
driver.findElement(By.id("email")) | Searches the current page's DOM for the first element whose id attribute is "email", and returns it as a WebElement. |
.sendKeys("tester@example.com") | Types the given text into that element, character by character, exactly as a keyboard would. |
driver.getTitle() | Reads the current page's <title> text — useful for quick sanity checks. |
driver.quit(); | Closes every window the driver opened and ends the browser session/process. Without this, browser and driver processes can pile up in the background. |
Why the try/finally Matters
If driver.findElement(...) throws an exception (say, the element isn't on the page
yet), and you don't wrap the code in try/finally, execution jumps straight out of the
method and driver.quit() never runs. The Chrome window — and its background
chromedriver process — stays open, silently consuming memory. Wrapping the whole
test body in try { ... } finally { driver.quit(); } guarantees cleanup happens whether
the test passes, fails, or throws.
WebDriver extends
AutoCloseable, so you can also write
try (WebDriver driver = new ChromeDriver()) { ... } and Java will call
driver.quit() for you automatically when the block exits — no explicit
finally needed. Both patterns are common in real codebases; use whichever your team's
style favors.Running It
Run FirstTest.main() from your IDE, or via Maven from the command line:
mvn compile exec:java -Dexec.mainClass="com.recipebox.tests.FirstTest"
You should see a real Chrome window flash open, navigate to the login page, type into the email field, print the page title to the console, then close.
By.id("email") is just one way to locate an element.
Head to ID, Name, ClassName, TagName to learn the other basic
locator strategies and when to reach for each one.