Essential Selenium WebDriver Operations for Web Automation Testing

1) Getting Page Title

String pageTitle = browserDriver.getTitle();

2) Getting Curent URL

String currentUrl = browserDriver.getCurrentUrl();

Browser Window Management

1) Maximizing Browser Window

browserDriver.manage().window().maximize();

2) Setting Custom Browser Dimensions

browserDriver.manage().window().setSize(new Dimension(800, 600));

3) Browser Navigation Conrtols

// Navigate back to previous page
browserDriver.navigate().back();

// Navigate forward to next page
browserDriver.navigate().forward();

// Navigate to a specific URL
browserDriver.navigate().to("https://www.google.com/");

4) Controlling Page Scroll Position

// Scroll to bottom of page
((JavascriptExecutor)browserDriver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

// Scroll to top of page
((JavascriptExecutor)browserDriver).executeScript("window.scrollTo(0, 0)");

Keyboard Interaction Handling

1) Simulating Single Key Press

// Simulate Enter key press
browserDriver.findElement(By.id("searchInput")).sendKeys(Keys.ENTER);

2) Implementing Keyboard Shortcuts

// Select all text (Ctrl+A)
browserDriver.findElement(By.id("searchInput")).sendKeys(Keys.CONTROL, "a");

// Cut selected text (Ctrl+X)
browserDriver.findElement(By.id("searchInput")).sendKeys(Keys.CONTROL, "x");

// Paste clipboard content (Ctrl+V)
browserDriver.findElement(By.id("searchInput")).sendKeys(Keys.CONTROL, "v");

Mouse Event Simulation

// Create an Actions object for mouse operations
Actions mouseActions = new Actions(browserDriver);
Thread.sleep(3000);

// Locate the target element
WebElement hoverTarget = browserDriver.findElement(By.cssSelector(".menu-item:nth-child(3)"));
Thread.sleep(3000);

// Move to element and perform right-click
mouseActions.moveToElement(hoverTarget).contextClick().perform();

Window Management

// Store the current window handle
String originalWindow = browserDriver.getWindowHandle();

// Get all window handles
Set<String> allWindows = browserDriver.getWindowHandles();
String destinationWindow = "";

// Iterate through all window handles
for (String windowHandle : allWindows) {
    destinationWindow = windowHandle;
}

// Switch to the last window
browserDriver.switchTo().window(destinationWindow);

Capturing Screenshots

First, add the commons-io dependency to your pom.xml:

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.16.1</version>
</dependency>
// Capture screenshot as file
File screenshotFile = ((TakesScreenshot)browserDriver).getScreenshotAs(OutputType.FILE);

// Save the file to disk
FileUtils.copyFile(screenshotFile, new File("/path/to/screenshots/test_screenshot.png"));

Tags: Selenium webdriver web-automation java-testing browser-automation

Posted on Fri, 12 Jun 2026 17:37:24 +0000 by messels