Selenium WebDriver Cheatsheet: Learn in 30 Minutes
If you’re always looking to learn something new at night and only have a short time, this Selenium WebDriver cheatsheet will help. If you’re getting ready for an interview, this quick guide will let you revise the basics in a flash. If you’re an automation newbie or a more experienced manual tester beginning with Selenium WebDriver in Java, this guide explains all the important features you need. The material is explained using simple language, not too many words and plenty of examples — not confusing at all.
📋 Quick Cheatsheet Overview
Selenium WebDriver is what?
Selenium WebDriver is a free tool used to automate tests for web applications. It manages the browser and makes it act as if a person is using the website.
Let’s picture it this way: you use Chrome, access a website, hit a button, fill in a form. WebDriver, too, follows the same steps, just much more quickly and consistently.
🔧 How to Set Up Selenium (Java)
Tools Required:
- Java (Install JDK)
- Eclipse or IntelliJ IDEA
- Selenium Java libraries (if you are using Maven, it's a cherry on top. You can easily add those by adding dependencies)
- ChromeDriver (or any browser driver) — Note: Not needed after Selenium 4. Easy peasy!
Maven Dependency:
<dependency>
<groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.10.0</version></dependency>
Basic Structure of a Selenium test:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyFirstTest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com");
driver.quit(); } }
Explanation:
The browser is started when WebDriver driver is called.
get() - Opens a URL.
Quitting closes the whole program.
Is WebDriver a class or an interface? — It’s an interface. Selenium provides multiple implementations of WebDriver for different browsers.
What does RemoteWebDriver do? It’s a class that implements WebDriver and is used to execute tests on remote machines using Selenium Grid.
What's the difference between quit() and close() ? - close() shuts the current tab, but quit() closes all tabs and ends the session.
How to Find Elements
driver.findElement(By.id("username"));
driver.findElement(By.name("password"));
driver.findElement(By.className("btn"));
driver.findElement(By.tagName("input"));
driver.findElement(By.linkText("Login"));
driver.findElement(By.partialLinkText("Log"));
driver.findElement(By.xpath("//input[@name='email']"));
driver.findElement(By.cssSelector("input[type='text']"));
Tip: Remember, XPath and CSS Selector tools are best if working with complex elements. The best locator is id as it is really fast and is sure to work every time. If this element is absent, use CSS or XPath depending on the website’s format.
How to interact with Web Elements
driver.findElement(By.id("email")).sendKeys("test@example.com");
driver.findElement(By.id("submit")).click();
driver.findElement(By.name("password")).clear();
String text = driver.findElement(By.tagName("h1")).getText();
Note: Always check if the element is visible and clickable.
Pro Tip: Create a helper class with reusable methods for getting text, waits, visibility checks, etc.
How do you handle dynamic web elements? — Use dynamic XPath/CSS, JavaScriptExecutor, or wait strategies.
Difference between findElement vs findElements? — findElement returns a single WebElement; throws error if not found. findElements returns a list (can be empty); no exception.
Syntax for the findElements() —
List<WebElement> elements = driver.findElements(By.className("item"));
Waits in Selenium
There are 2 common types of waits:
1. Implicit Wait:
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
2. Explicit Wait:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcome")));
FluentWait wait = new FluentWait(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
Tip during an interview: Difference between implicit, explicit, and fluent wait?
Implicit waits apply globally, not specific.
Explicit waits are more powerful — used for specific conditions.
Fluent wait gives full control over polling time and exception handling.
What do you think is the best one? Most of the time, use the explicit wait instead of an implicit wait.
🔐 Managing Alerts, Frames and Windows
// Handle Alerts
alert alert = driver.switchTo.alert();
alert.accept();
// Switch Frames
driver.switchTo().frame("frameName");
// Window Handling
String parent = driver.getWindowHandle();
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
How to handle multiple tabs/windows? — Use getWindowHandles() and switch using loop.
How to handle alerts? — Use switchTo().alert() and methods like accept(), dismiss().
Types of frames? — You can switch using index, name/id, or WebElement.
Difference between getWindowHandle vs getWindowHandles? — First returns a single window ID (String); second returns a Set of all IDs.
🗃️ Useful Browser Actions
driver.navigate().to("https://example.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
Interview Tip:
How do you handle cross-browser testing? — Use WebDriverManager or Selenium Grid with different browser drivers.
How to set headless Chrome? —
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);
📊 Validations & Assertions
String title = driver.title();System.out.println(title.equals("Expected Title"));String url = driver.getUrl();System.out.println(url.contains("expected-part"));
Interview Tip:
Difference between assert vs verify? — Assert stops execution on failure. Verify logs the failure and continues.
Soft assert vs hard assert? — Soft assert doesn’t halt the script. Hard assert does.
Is it actual vs expected or expected vs actual? — Always: expected vs actual for clarity.
Real Life Tip
When I started, I kept a sticky note near my laptop with five lines of code that launched the browser and opened Google. I used to run that every night. That tiny habit built my confidence more than any tutorial did.
📆 Challenge Yourself
Try automating the login form of a demo site like:
🔗 https://www.saucedemo.com/v1/
Write a script to:
Open the site
Enter username and password
Click Login
Print success/failure message
If successful, complete the eCommerce flow by adding products and checking out.
Final Thoughts
This cheatsheet is designed to quickly help night owls and those studying for interviews understand Selenium WebDriver. Begin with these simple steps and add more as you go. Keep it fun — break things, fix them, learn.
If this guide helped you, share it with your team or friends on WhatsApp, LinkedIn or Twitter.
Until next time — keep testing, keep learning, and sip that chai! ☕
