</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

What are the new features in Selenium 4 compared to Selenium 3?

Answer

Selenium 4 Key New Features

1. W3C WebDriver Standard (No JSON Wire Protocol)

Java
// Selenium 3 — JSON Wire Protocol (custom, not W3C)
// Selenium 4 — W3C WebDriver standard natively
// Impact: Better cross-browser compatibility, no capability translation needed

// Capabilities now use standard W3C format
ChromeOptions options = new ChromeOptions();
options.setCapability("browserVersion", "latest");  // W3C standard key

2. Relative Locators (Selenium 4 Only)

Find elements based on their position relative to other elements:

Java
import static org.openqa.selenium.support.locators.RelativeLocator.with;

// Element ABOVE another
WebElement labelAbove = driver.findElement(
    with(By.tagName("label")).above(By.id("password")));

// Element BELOW another
WebElement helpText = driver.findElement(
    with(By.className("hint")).below(By.id("username")));

// Element to the LEFT
WebElement label = driver.findElement(
    with(By.tagName("label")).toLeftOf(By.id("email")));

// Element to the RIGHT
WebElement icon = driver.findElement(
    with(By.tagName("img")).toRightOf(By.id("email")));

// Element NEAR (within 50px) another
WebElement nearBtn = driver.findElement(
    with(By.tagName("button")).near(By.id("submit")));

3. Chrome DevTools Protocol (CDP) Integration

Java
// Selenium 4 provides native access to Chrome DevTools Protocol
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();

// Mock geolocation
devTools.send(Emulation.setGeolocationOverride(
    Optional.of(37.7749),    // lat (San Francisco)
    Optional.of(-122.4194),  // long
    Optional.of(1.0)));

// Simulate network throttling
devTools.send(Network.emulateNetworkConditions(
    false, 100, 50000, 50000,
    Optional.of(Network.ConnectionType.WIFI)));

// Capture network logs
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTools.addListener(Network.requestWillBeSent(), request ->
    System.out.println("URL: " + request.getRequest().getUrl()));

// Set basic auth (no alert handling needed!)
devTools.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
((HasAuthentication) driver).register(
    UsernameAndPassword.of("admin", "password"));

4. New Window and Tab API

Java
// Open NEW tab (clean Selenium 4 API — no JS tricks needed)
driver.switchTo().newWindow(WindowType.TAB);
driver.get("https://example.com");

// Open NEW browser window
driver.switchTo().newWindow(WindowType.WINDOW);
driver.get("https://example.com");

5. Improved Screenshots

Java
// Selenium 3: screenshot of full page only
// Selenium 4: screenshot of individual ELEMENT

WebElement card = driver.findElement(By.cssSelector(".product-card"));
File cardScreenshot = card.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(cardScreenshot, new File("card.png"));

// Full page still works
File fullPage = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

6. Selenium Grid 4 — Fully Revamped

Bash
# Grid 4 — single JAR, multiple modes
java -jar selenium-server.jar standalone      # Hub + Node in one
java -jar selenium-server.jar hub             # Hub only
java -jar selenium-server.jar node            # Node only

# Grid UI at localhost:4444/ui
# Observability: OpenTelemetry tracing, distributed logging
# Docker-ready out of the box

7. Deprecations Removed in Selenium 4

Java
// Selenium 3 (deprecated in 4)
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);   // old

// Selenium 4 (use Duration)
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); // new

// DesiredCapabilities replaced by browser-specific Options
// DesiredCapabilities caps = new DesiredCapabilities();  ← Selenium 3
ChromeOptions options = new ChromeOptions();              // ← Selenium 4

Quick Migration: Selenium 3 → 4

ChangeSelenium 3Selenium 4
CapabilitiesDesiredCapabilitiesBrowserOptions (ChromeOptions etc.)
TimeoutsTimeUnit.SECONDSDuration.ofSeconds()
GridHub-Node separate JARsSingle JAR, multiple modes
ScreenshotsPage onlyElement-level supported
Relative locatorsNot availablewith(By.x).above(By.y)
CDPNot availableNative support

Follow AutomateQA

Related Topics