</>

Technology

Selenium

Difficulty

Advanced

Interview Question

What are the new features introduced in Selenium 4?

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 Options classes
  • DesiredCapabilities deprecated for browser-specific ChromeOptions, 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

FeatureSelenium 3Selenium 4
LocatorsStandard only+ Relative Locators
CDP accessVia third-partyNative built-in
GridHub/Node onlyStandalone/Hub/Node/Distributed
New windowVia JSswitchTo().newWindow()
W3C compliancePartialFull
Element screenshotFull page onlyElement-level

Follow AutomateQA

Related Topics