Enough theory. Let's write your first Selenium script. This code launches Chrome, goes to a URL, prints the page title, and closes the browser. 5 lines of actual code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstSeleniumTest {
public static void main(String[] args) {
// Launch Chrome (Selenium Manager handles driver automatically)
WebDriver driver = new ChromeDriver();
// Go to the Banking Portal
driver.get("https://www.testerrank.com/banking");
// Print the page title
System.out.println("Title: " + driver.getTitle());
// Print the current URL
System.out.println("URL: " + driver.getCurrentUrl());
// Close the browser and end the session
driver.quit();
}
}Run this. A Chrome window opens, navigates to the Banking Portal, prints the title in your console, and closes. That's it. You just automated a browser.
Always use driver.quit() at the end. It closes all windows and kills the ChromeDriver process. If you use driver.close() instead, it only closes the current tab — the ChromeDriver process stays alive and eats memory. After 10 test runs without quit(), your machine slows to a crawl.
In real projects, you don't just launch Chrome with defaults. You need headless mode for CI/CD, you need to disable popups, set window size, etc. ChromeOptions handles all of this.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
ChromeOptions options = new ChromeOptions();
// Headless mode — no visible browser window
// Essential for Jenkins/CI pipelines where there's no display
options.addArguments("--headless=new");
// Set window size (important in headless — default is tiny)
options.addArguments("--window-size=1920,1080");
// Disable the "Chrome is being controlled" bar
options.setExperimentalOption("excludeSwitches",
java.util.List.of("enable-automation"));
// Disable notification popups
options.addArguments("--disable-notifications");
// Disable password save prompts
java.util.Map<String, Object> prefs = new java.util.HashMap<>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
// Start maximized
options.addArguments("--start-maximized");
// Pass options to ChromeDriver
WebDriver driver = new ChromeDriver(options);Same code, different class name. The WebDriver API is identical across all browsers — that's the beauty of it.
// Firefox
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
FirefoxOptions ffOptions = new FirefoxOptions();
ffOptions.addArguments("--headless");
WebDriver ffDriver = new FirefoxDriver(ffOptions);
// Edge
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.addArguments("--headless=new");
WebDriver edgeDriver = new EdgeDriver(edgeOptions);
// Safari (macOS only, no headless)
import org.openqa.selenium.safari.SafariDriver;
WebDriver safariDriver = new SafariDriver();WebDriver driver = new ChromeDriver();
// Go to a URL (waits for page to load)
driver.get("https://www.testerrank.com/banking");
// Same thing, different syntax
driver.navigate().to("https://www.testerrank.com/banking/login");
// Browser Back button
driver.navigate().back();
// Browser Forward button
driver.navigate().forward();
// Refresh the page
driver.navigate().refresh();
// Maximize the window
driver.manage().window().maximize();
// Set specific window size
driver.manage().window().setSize(
new org.openqa.selenium.Dimension(1366, 768));Q: What is the difference between driver.get() and driver.navigate().to()?
A: Both navigate to a URL and wait for the page to load. The difference is that navigate() also gives you access to back(), forward(), and refresh() methods — like browser history controls. Functionally, for navigating to a URL, they behave the same.
Q: What is the difference between driver.close() and driver.quit()?
A: driver.close() closes only the current window/tab. If you have multiple windows open, the WebDriver session stays alive. driver.quit() closes ALL windows and terminates the WebDriver session — the ChromeDriver process is killed. Always use quit() at the end of your test to prevent resource leaks.
Q: What is headless mode and when would you use it?
A: Headless mode runs the browser without a visible GUI. The browser still renders pages and executes JavaScript, but nothing appears on screen. Use it in CI/CD pipelines (Jenkins, GitHub Actions) where there is no display, and for faster execution since no rendering to screen is needed. Set it with options.addArguments("--headless=new").
Key Point: new ChromeDriver() opens Chrome. driver.get(url) navigates. driver.quit() closes everything. That's your starting point for every test.