⚙️ Setup & Installation Beginner
Maven Dependencies
<!-- Selenium Java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.15.0</version> </dependency>
<!-- WebDriverManager (Auto-download drivers) -->
Initialize WebDriver
import org.openqa.selenium.WebDriver; import
org.openqa.selenium.chrome.ChromeDriver;
import
io.github.bonigarcia.wdm.WebDriverManager;
// Option 1: Using WebDriverManager (Recommended)new
ChromeDriver();
// Option 2: Manual driver path
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver"); WebDriver driver = new
ChromeDriver();
// Other browsers
WebDriver firefoxDriver = new
FirefoxDriver(); WebDriver edgeDriver = new EdgeDriver(); WebDriver safariDriver = new
SafariDriver();
Browser Options
ChromeOptions options = new
ChromeOptions();
// Headless mode
options.addArguments("--headless");
// Window size
options.addArguments("--window-size=1920,1080"); options.addArguments("--start-maximized"// Disable notifications
options.addArguments("--disable-notifications"// Disable automation flags
options.addArguments("--disable-blink-features=AutomationControlled"); options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"// Incognito mode
options.addArguments("--incognito");
// Accept insecure certificates
options.setAcceptInsecureCerts(true); WebDriver driver = new
ChromeDriver(options);
📘 Basic Commands Beginner
Navigation
// Open URL
driver.get("https://example.com");
// Navigate methods
driver.navigate().to("https://example.com"// Get current URL
String currentUrl = driver.getCurrentUrl();
// Get page title
String title = driver.getTitle();
// Get page source
String pageSource = driver.getPageSource();
Window Management
// Maximize window
driver.manage().window().maximize();
// Minimize window
driver.manage().window().minimize();
// Fullscreen
driver.manage().window().fullscreen();
// Set window size
driver.manage().window().setSize(new
Dimension(1920, 1080));
// Set window position
driver.manage().window().setPosition(new0, 0));
// Get window handle
String windowHandle = driver.getWindowHandle();
// Get all window handles
Set<String> handles = driver.getWindowHandles();
Browser Cleanup
// Close current window
driver.close();
// Close all windows and quit driver
driver.quit();
💡 Best Practice: Always use
driver.quit() in teardown methods to close all windows
and end the session properly.
🎯 Element Locators Beginner
Find Element Methods
import org.openqa.selenium.By; import org.openqa.selenium.WebElement;
// By ID (fastest and most reliable)
WebElement element = driver.findElement(By.id("username"// By Name
WebElement element = driver.findElement(By.name("email"// By Class Name
WebElement element = driver.findElement(By.className(// By Tag Name
WebElement element = driver.findElement(By.tagName(// By Link Text (exact match)
WebElement element = driver.findElement(By.linkText(// By Partial Link Text
WebElement element = driver.findElement(By.partialLinkText(// By CSS Selector (flexible and fast)
WebElement element = driver.findElement(By.cssSelector(// By XPath (powerful but slower)
WebElement element = driver.findElement(By.xpath(
CSS Selector Examples
| Selector | Description | Example |
|---|---|---|
#id |
ID selector | #username |
.class |
Class selector | .btn-primary |
tag |
Tag selector | input |
[attribute] |
Attribute exists | [required] |
[attr='value'] |
Exact match | [type='text'] |
[attr^='value'] |
Starts with | [id^='user'] |
[attr$='value'] |
Ends with | [id$='name'] |
[attr*='value'] |
Contains | [class*='btn'] |
parent > child |
Direct child | div > input |
ancestor descendant |
Any descendant | form input |
:nth-child(n) |
Nth child | li:nth-child(2) |
:first-child |
First child | li:first-child |
:last-child |
Last child | li:last-child |
XPath Examples
// Absolute XPath (not recommended - brittle)"/html/body/div/form/input"
// Relative XPath (recommended)
"//input[@id='username']"
// By text content
"//button[text()='Submit']"
"//a[contains(text(),'Click')]"
// By attribute
"//input[@type='password']"
"//div[@class='container']"
// Multiple attributes (AND)
"//input[@type='text' and @name='username']"// OR condition
"//input[@type='submit' or @type='button']"// Contains attribute value
"//div[contains(@class,'btn')]"
// Starts with
"//input[starts-with(@id,'user')]"
// Parent axis
"//input[@id='username']/parent::div"
// Following sibling
"//label[@for='username']/following-sibling::input"// Preceding sibling
"//input[@id='password']/preceding-sibling::label"// Index-based
"(//input[@type='text'])[2]"
Finding Multiple Elements
// Find multiple elements
List<WebElement> elements =
driver.findElements(By.className("item"));
// Iterate through elements
for (WebElement element : elements) { System.out.println(element.getText()); }
// Get count
int count = elements.size();
💡 Locator Strategy Priority:
1. ID (fastest, most reliable)
2. CSS Selector (fast, flexible)
3. XPath (powerful but slower)
4. Avoid: linkText, className if possible
1. ID (fastest, most reliable)
2. CSS Selector (fast, flexible)
3. XPath (powerful but slower)
4. Avoid: linkText, className if possible
🖱️ Element Interactions Intermediate
Basic Interactions
// Click element
element.click();
// Type text (appends to existing)
element.sendKeys("Hello World");
// Clear and type
element.clear(); element.sendKeys("New Text"// Submit form
element.submit();
// Get text
String text = element.getText();
// Get attribute value
String value = element.getAttribute("value"); String href = element.getAttribute("href"// Get CSS property
String color = element.getCssValue("color"); String fontSize = element.getCssValue("font-size"// Check if element is displayed
boolean isDisplayed = element.isDisplayed();
// Check if element is enabled
boolean isEnabled = element.isEnabled();
// Check if checkbox/radio is selected
boolean isSelected = element.isSelected();
// Get element size
Dimension size = element.getSize();
int width = size.getWidth(); int height = size.getHeight();
// Get element location
Point location = element.getLocation();
int x = location.getX(); int y = location.getY();
Dropdown/Select Elements
import
org.openqa.selenium.support.ui.Select;
// Create Select object
Select dropdown = new
Select(driver.findElement(By.id(// Select by visible text
dropdown.selectByVisibleText("United States"// Select by value attribute
dropdown.selectByValue("us");
// Select by index (0-based)
dropdown.selectByIndex(2);
// Get selected option
WebElement selected = dropdown.getFirstSelectedOption(); String
selectedText = selected.getText();
// Get all options
List<WebElement> options = dropdown.getOptions();
// Deselect (for multi-select)
dropdown.deselectAll(); dropdown.deselectByValue(); dropdown.deselectByIndex(0);
// Check if multi-select
boolean isMultiple = dropdown.isMultiple();
Checkboxes and Radio Buttons
// Select checkbox/radio
if (!element.isSelected()) { element.click(); }
// Uncheck checkbox
if (element.isSelected()) { element.click(); }
// Toggle checkbox
element.click();
File Upload
// Upload file (no click needed)
WebElement uploadElement = driver.findElement(By.id()); uploadElement.sendKeys("/path/to/file.pdf"// Multiple files (some browsers)
uploadElement.sendKeys("/path/file1.pdf\n/path/file2.pdf"
Actions Class (Advanced Interactions)
import
org.openqa.selenium.interactions.Actions; Actions actions =
new
Actions(driver);
// Right click
actions.contextClick(element).perform();
// Double click
actions.doubleClick(element).perform();
// Hover over element
actions.moveToElement(element).perform();
// Drag and drop
WebElement source = driver.findElement(By.id("draggable")); WebElement target = driver.findElement(By.id(// Drag by offset
actions.dragAndDropBy(element, 100, 200).perform();
// Click and hold
actions.clickAndHold(element).perform();
// Release
actions.release().perform();
// Chain actions
actions .moveToElement(menu) .click() .moveToElement(submenu)
.click() .perform();
// Send keyboard keys
actions.sendKeys(Keys.ENTER).perform();
actions.sendKeys(Keys.TAB).perform();
// Key combinations
actions.keyDown(Keys.CONTROL).sendKeys("a"
JavaScript Execution
import
org.openqa.selenium.JavascriptExecutor; JavascriptExecutor js =
(JavascriptExecutor) driver;
// Click element (bypasses visibility checks)"arguments[0].click();", element);
// Scroll to element
js.executeScript("arguments[0].scrollIntoView(true);"// Scroll to bottom
js.executeScript("window.scrollTo(0, document.body.scrollHeight);"// Scroll by pixels
js.executeScript("window.scrollBy(0, 500);"// Set input value
js.executeScript("arguments[0].value='text';"// Get element text (including hidden)
String text = (String) js.executeScript("return arguments[0].innerText;"// Highlight element (debugging)
js.executeScript("arguments[0].style.border='3px solid red'"// Remove element attribute
js.executeScript("arguments[0].removeAttribute('readonly');"// Check page load state
String state = (String) js.executeScript("return document.readyState"
⏰ Waits & Synchronization Intermediate
Implicit Wait
import java.time.Duration;
// Set implicit wait (applies to all findElement calls)// Not recommended to use with explicit waits
⚠️ Warning: Don't mix implicit and explicit waits.
Choose one strategy and stick with it.
Explicit Wait (Recommended)
import
org.openqa.selenium.support.ui.WebDriverWait;
import
org.openqa.selenium.support.ui.ExpectedConditions;
// Create wait object
WebDriverWait wait = new
WebDriverWait(driver, Duration.ofSeconds(10));
// Wait for element to be visible
WebElement element = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.id(// Wait for element to be clickable
wait.until(ExpectedConditions.elementToBeClickable(By.id(// Wait for element to be present
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(// Wait for element to be invisible
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id(// Wait for text to be present
wait.until(ExpectedConditions.textToBePresentInElementLocated(
By.id("message"), "Success"
));
// Wait for title
wait.until(ExpectedConditions.titleIs("Home Page")); wait.until(ExpectedConditions.titleContains(// Wait for URL
wait.until(ExpectedConditions.urlToBe("https://example.com/home")); wait.until(ExpectedConditions.urlContains("home"// Wait for alert
wait.until(ExpectedConditions.alertIsPresent());
// Wait for frame to be available
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(// Wait for number of windows
wait.until(ExpectedConditions.numberOfWindowsToBe(// Wait for staleness (element no longer in DOM)// Wait for attribute value
wait.until(ExpectedConditions.attributeToBe(element,
"class", "active"));
wait.until(ExpectedConditions.attributeContains(element,
"class", "btn"));
Fluent Wait (Advanced)
import
org.openqa.selenium.support.ui.FluentWait;
import
org.openqa.selenium.NoSuchElementException;
// Create fluent wait with custom pollingnew
FluentWait<>(driver) .withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofMillis(500)) .ignoring(NoSuchElementException.class// Use fluent wait
WebElement element = fluentWait.until(driver"dynamic-element"
Thread Sleep (Not Recommended)
// Hard wait - avoid if possible
Thread.sleep(3000); // 3 seconds
⚠️ Avoid Thread.sleep(): Use explicit waits
instead. Hard sleeps slow down tests and make them brittle.
Page Load Timeout
// Set page load timeout
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(// Set script timeout (for async scripts)
🚀 Advanced Features Advanced
Handling Alerts/Popups
import org.openqa.selenium.Alert;
// Switch to alert
Alert alert = driver.switchTo().alert();
// Get alert text
String alertText = alert.getText();
// Accept alert (click OK)
alert.accept();
// Dismiss alert (click Cancel)
alert.dismiss();
// Send text to prompt
alert.sendKeys("My Input"); alert.accept();
// Wait for alert
WebDriverWait wait = new
WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.alertIsPresent());
Handling Frames/iFrames
// Switch to frame by index
driver.switchTo().frame(0);
// Switch to frame by name or ID
driver.switchTo().frame("frameName");
// Switch to frame by WebElement
WebElement frameElement = driver.findElement(By.id(// Switch back to main content
driver.switchTo().defaultContent();
// Switch to parent frame
driver.switchTo().parentFrame();
Handling Multiple Windows/Tabs
// Get current window handle
String mainWindow = driver.getWindowHandle();
// Open new tab (Selenium 4+)
driver.switchTo().newWindow(WindowType.TAB);
// Open new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Get all window handles
Set<String> allWindows = driver.getWindowHandles();
// Switch to new window
for (String window : allWindows) { if (!window.equals(mainWindow)) {
driver.switchTo().window(window);
break; } }
// Close current window and switch back
driver.close(); driver.switchTo().window(mainWindow);
// Wait for new window
wait.until(ExpectedConditions.numberOfWindowsToBe(
Screenshots
import
org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType; import org.apache.commons.io.FileUtils;
// Take screenshot
TakesScreenshot ts = (TakesScreenshot) driver; File source =
ts.getScreenshotAs(OutputType.FILE); File destination =
new File("screenshot.png"// Take element screenshot (Selenium 4+)"logo"new File("element.png"// Screenshot as Base64
String base64 = ts.getScreenshotAs(OutputType.BASE64);
Cookies Management
import org.openqa.selenium.Cookie;
// Add cookie
Cookie cookie = new Cookie(, "value"); driver.manage().addCookie(cookie);
// Get cookie by name
Cookie myCookie = driver.manage().getCookieNamed(// Get all cookies
Set<Cookie> cookies = driver.manage().getCookies();
// Delete cookie
driver.manage().deleteCookieNamed("sessionId"// Delete all cookies
driver.manage().deleteAllCookies();
Browser Logs
import
org.openqa.selenium.logging.LogType;
import
org.openqa.selenium.logging.LogEntries;
import
org.openqa.selenium.logging.LogEntry;
// Get browser console logs
LogEntries logs = driver.manage().logs().get(LogType.BROWSER);
for (LogEntry entry : logs) { System.out.println(entry.getLevel() +
" " + entry.getMessage()); }
// Available log types
driver.manage().logs().get(LogType.BROWSER);
driver.manage().logs().get(LogType.DRIVER);
driver.manage().logs().get(LogType.CLIENT);
driver.manage().logs().get(LogType.SERVER);
Network Interception (Selenium 4+)
import
org.openqa.selenium.devtools.DevTools;
import
org.openqa.selenium.devtools.v119.network.Network;
// Enable DevTools
DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
// Enable network tracking
devTools.send(Network.enable(Optional.empty(), Optional.empty(),
Optional.empty()));
// Intercept requests
devTools.addListener(Network.requestWillBeSent(), request -> {
System.out.println("Request: " + request.getRequest().getUrl()); });
// Intercept responses
devTools.addListener(Network.responseReceived(), response -> {
System.out.println("Response: " + response.getResponse().getUrl()); });
Geolocation (Selenium 4+)
// Set geolocation
driver.executeCdpCommand("Emulation.setGeolocationOverride", Map.of("latitude", 37.7749,
"longitude", -122.4194,
"accuracy", 1) );
Mobile Emulation
import java.util.HashMap;
// Emulate mobile device
Map<String, Object> mobileEmulation =
new HashMap<>(); mobileEmulation.put("deviceName",
"iPhone 12 Pro"); ChromeOptions options = new
ChromeOptions(); options.setExperimentalOption(new
ChromeDriver(options);
// Custom mobile settings
Map<String, Object> deviceMetrics =
new HashMap<>(); deviceMetrics.put("width",
375); deviceMetrics.put(, 812); deviceMetrics.put(, 3.0); mobileEmulation.put(, deviceMetrics); mobileEmulation.put("userAgent""Mozilla/5.0 (iPhone; CPU iPhone OS 15_0..."
✨ Best Practices & Tips
Page Object Model (POM)
import
org.openqa.selenium.support.FindBy;
import
org.openqa.selenium.support.PageFactory;
public class LoginPage { private WebDriver driver; @FindBy(id = "username") private WebElement usernameField;
@FindBy(id = "password") private WebElement passwordField;
@FindBy(css = "button[type='submit']") private WebElement loginButton;
public LoginPage(WebDriver driver) { this.driver = driver; PageFactory.initElements(driver, this); }
public void login(String username, String password) { usernameField.sendKeys(username);
passwordField.sendKeys(password); loginButton.click(); } }
✅ DO:
- Use explicit waits instead of implicit
- Prefer ID and CSS selectors over XPath
- Always call driver.quit() in @AfterMethod
- Use Page Object Model for maintainability
- Keep locators in Page Objects, not in tests
- Use WebDriverManager for driver management
- Create reusable BasePage class
- Handle exceptions appropriately
❌ DON'T:
- Don't use Thread.sleep() for waits
- Don't mix implicit and explicit waits
- Don't use absolute XPath
- Don't hardcode test data in test methods
- Don't leave driver instances unclosed
- Don't catch exceptions without logging
- Don't create too many dependencies between tests
Exception Handling
try { WebElement element = driver.findElement(By.id("submit"));
element.click(); }
catch (NoSuchElementException e) { System.out.println("Element not found");
}
catch (ElementNotInteractableException e) { System.out.println(catch (StaleElementReferenceException e) { System.out.println(); element = driver.findElement(By.id("submit"catch (TimeoutException e) { System.out.println(
Common Selenium Exceptions
| Exception | Cause | Solution |
|---|---|---|
| NoSuchElementException | Element not found | Check locator, add wait |
| StaleElementReferenceException | Element removed from DOM | Re-locate element |
| ElementNotInteractableException | Element not clickable | Wait for clickable, scroll |
| TimeoutException | Wait timeout exceeded | Increase timeout, check condition |
| NoSuchWindowException | Window closed | Check window handles |
| NoAlertPresentException | No alert present | Wait for alert |
Performance Tips
🚀 Speed Up Your Tests:
- Use headless mode for CI/CD
- Disable images/CSS loading (if not needed)
- Run tests in parallel
- Use explicit waits with appropriate timeouts
- Minimize browser actions (group operations)
- Cache frequently used elements
- Use CSS selectors instead of XPath