Answer
New Features in Selenium 4
Selenium 4 is a major release with significant architectural and API improvements over Selenium 3.
1. Relative Locators
Find elements relative to other elements — great for dynamic grids:
Java
import static org.openqa.selenium.support.locators.RelativeLocator.*;
// Find password field that is BELOW username field
WebElement username = driver.findElement(By.id("username"));
WebElement password = driver.findElement(
with(By.tagName("input")).below(username)
);
// Find label to the LEFT of an input
WebElement label = driver.findElement(
with(By.tagName("label")).toLeftOf(By.id("email"))
);
// Find element NEAR another (within 50px)
WebElement helpIcon = driver.findElement(
with(By.tagName("span")).near(By.id("phoneField"))
);
▸Available:
above(), below(), toLeftOf(), toRightOf(), near()2. Native Chrome DevTools Protocol (CDP)
Direct access to Chrome DevTools — intercept network, emulate devices, mock geolocation:
Java
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
// Enable network interception
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
// Mock geolocation
devTools.send(Emulation.setGeolocationOverride(
Optional.of(40.7128), Optional.of(-74.0060), Optional.of(1)
));
// Intercept requests
devTools.addListener(Network.requestWillBeSent(), request -> {
System.out.println("Request: " + request.getRequest().getUrl());
});
3. New Window / Tab API
Java
// Open a new tab and switch to it
driver.switchTo().newWindow(WindowType.TAB);
// Open a new browser window
driver.switchTo().newWindow(WindowType.WINDOW);
4. Selenium Grid 4 (Fully Rewritten)
- ✓Standalone, Hub-Node, and Distributed modes
- ✓Docker/Kubernetes native support
- ✓Built-in observability (GraphQL API, event bus)
- ✓No more JSON config files needed
Bash
# Standalone (all-in-one)
java -jar selenium-server.jar standalone
# Hub
java -jar selenium-server.jar hub
# Node
java -jar selenium-server.jar node --hub http://hub:4444
5. W3C WebDriver Standard (Full Compliance)
- ✓All capability handling moved to browser
Optionsclasses - ✓
DesiredCapabilitiesdeprecated for browser-specificChromeOptions,FirefoxOptions - ✓More consistent cross-browser behavior
6. Improved Screenshots
Java
// Screenshot of a specific element (not whole screen)
WebElement card = driver.findElement(By.cssSelector(".product-card"));
File screenshot = card.getScreenshotAs(OutputType.FILE);
7. Print Page to PDF
Java
PrintsPage printer = (PrintsPage) driver;
PrintOptions printOptions = new PrintOptions();
Pdf pdf = printer.print(printOptions);
String base64pdf = pdf.getContent();
Key Upgrade Summary
| Feature | Selenium 3 | Selenium 4 |
|---|---|---|
| Locators | Standard only | + Relative Locators |
| CDP access | Via third-party | Native built-in |
| Grid | Hub/Node only | Standalone/Hub/Node/Distributed |
| New window | Via JS | switchTo().newWindow() |
| W3C compliance | Partial | Full |
| Element screenshot | Full page only | Element-level |
