Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 35 ยท Selenium 4 Features

What Is New in Selenium 4

A tour of Selenium 4: W3C WebDriver compliance, relative locators, new tab and window API, element screenshots, DevTools access and BiDi, plus migration tips.

Selenium 4 was a large release. It changed how the client libraries talk to browsers, added a handful of features that remove old workarounds, and cleaned out calls that had piled up over a decade. If you learned Selenium from older books or blog posts, much of what you read still works, but some of it is now deprecated or gone. This lesson is a map of what changed, so you can read old code, write new code and know which tutorial advice to ignore.

Real browsers and drivers cannot run on this site, so all Selenium code below is read-only. The runnable examples are stand-ins written in plain JavaScript: a small checker that flags Selenium 3 style code, and a model of how relative locators pick elements from their positions. They show the logic, not the real Selenium library.

Selenium 4 in five ideas

W3C standard One official protocol, no dialect translation.
Relative locators Find elements by position on the page.
Tabs and windows Open and switch in one call.
DevTools access Reach Chrome and Edge internals.

BiDi, the two-way protocol, is the longer-term direction.

1 / 2

W3C WebDriver compliance

Every Selenium script talks to a browser driver (ChromeDriver, GeckoDriver, EdgeDriver) over HTTP. In the early years, Selenium used its own message format called the JSON Wire Protocol, and a second effort at the W3C turned the idea into a standard, the WebDriver specification. For a long time the client libraries had to speak both and translate between them, which caused odd differences from browser to browser.

Selenium 4 speaks the W3C protocol by default. In practice that means:

  • Commands and responses follow one official format, so a Selenium client and any compliant driver understand each other without a translation layer.
  • Browser vendors maintain the drivers against the same specification, so behavior such as clicking, typing and window handling is more consistent between Chrome, Firefox and Edge.
  • Capabilities (the settings you send when you start a session) follow the standard names, and anything vendor specific carries a prefix with a colon, such as goog:chromeOptions. You will build one in Headless Mode and Browser Options.

How the protocol works underneath is covered in How WebDriver Works. The takeaway here is simple: you rarely write protocol messages yourself, but if you debug a failing session you will see W3C style requests.

Options classes replace DesiredCapabilities

Selenium 3 code often built a DesiredCapabilities object and passed it to the driver. In Selenium 4 you use a browser specific options class instead, and each class knows the settings of its browser:

Python
# read-only: Selenium 4 style (Python)
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--window-size=1280,800")
driver = webdriver.Chrome(options=options)
Java
// read-only: Selenium 4 style (Java)
ChromeOptions options = new ChromeOptions();
options.addArguments("--window-size=1280,800");
WebDriver driver = new ChromeDriver(options);

The old DesiredCapabilities pattern is deprecated and has been removed in newer releases of some bindings. If an old tutorial uses it, translate it to the options class.

Relative locators

A relative locator finds an element by where it sits compared with other elements. Think of a login form where the label has no id, but you know the password field is the one below the email field. In Python you import locate_with and chain position words:

Python
# read-only
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with

email = driver.find_element(By.ID, "email")
password = driver.find_element(locate_with(By.TAG_NAME, "input").below(email))

The methods are above, below, to_left_of, to_right_of and near. You can pass an element you already found, or a locator. The near method looks for elements within a default distance of about 50 pixels, and you can give another distance. Java has the same idea through RelativeLocator.with(By.tagName("input")).below(email).

Under the hood, the browser computes each candidate's bounding box (its position and size on screen) and Selenium filters candidates by those boxes. That makes relative locators handy and also a little fragile: responsive layouts move elements, so the same page can answer differently at different window sizes. Use them for cases where there is no better handle, and read Choosing Stable Locators for the ranking.

The first stand-in models this filtering with rectangles.

JS Relative locators as geometry

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The model is a simplification, but it shows why a layout change can break such a locator: the answers come from geometry, not from the page structure.

New tabs and windows

Opening a second tab used to need a JavaScript trick and a handle juggling routine. Selenium 4 added one call that creates a tab or a window and switches to it:

Python
# read-only
driver.get("https://example.test/dashboard")
driver.switch_to.new_window("tab")      # or "window"
driver.get("https://example.test/reports")
print(driver.window_handles)            # two handles now

In Java it is driver.switchTo().newWindow(WindowType.TAB). You still need the handle list to go back to the first tab, as explained in Alerts, Frames and Windows.

Element screenshots, window controls and more

Taking a picture of one element, not the whole page, is useful for visual notes and failure artifacts. The Python call is element.screenshot("logo.png"), and Java has element.getScreenshotAs(OutputType.FILE). Firefox also offers a full-page screenshot through its own driver methods. File Uploads, Downloads and Screenshots goes deeper.

Other small additions you will meet in code:

  • driver.get_window_rect() and set_window_rect() read or change the window position and size together.
  • driver.minimize_window() joins the existing maximize_window() and fullscreen_window().
  • Selenium Manager, an automatic driver finder that ships with the library from around version 4.6, so you no longer download ChromeDriver by hand. See Setup and Your First Script.
  • A redesigned Grid with a new architecture and a dashboard (see Selenium Grid).

DevTools and BiDi

Chromium based drivers can pass Chrome DevTools Protocol (CDP) commands straight through. That gives scripts access to features the classic protocol never had: network control, console events, device emulation. Selenium 4 wraps this access, and the next lesson, DevTools Protocol and BiDi, shows examples. WebDriver BiDi is the standard bi-directional successor: the browser can push events to the script instead of only answering commands. Support grows release by release and differs between languages, so check the documentation of your version before relying on a feature.

Reading old code: a migration checker

The second stand-in scans a few lines of old style code and suggests the Selenium 4 form. It is a plain pattern matcher, not a real linter, but it makes the changes concrete.

JS Spot Selenium 3 style code

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which protocol does Selenium 4 use by default to talk to browser drivers?

    Selenium 4 speaks the W3C WebDriver standard, so clients and drivers share one official message format.
  2. 2. How do you find the input that sits directly under a known element with a relative locator in Python?

    locate_with builds a locator from a normal By strategy, and methods such as below, above, near narrow it by position.
  3. 3. What does driver.switch_to.new_window('tab') do?

    It creates the tab (or a window, if you pass 'window') and moves the driver's focus there in one call.
  4. 4. Why can relative locators be fragile?

    They are computed from bounding boxes, so a responsive layout can move elements and change the result.
  5. 5. What replaces DesiredCapabilities in Selenium 4 code?

    Each browser has an options class that knows its own settings and builds the W3C capabilities for you.

Summary

  • Selenium 4 uses the W3C WebDriver protocol by default, which makes browsers behave more consistently.
  • Options classes replace DesiredCapabilities; wait and timeout calls take explicit durations; the old find_element_by_* helpers are gone in Python.
  • Relative locators (above, below, to_left_of, to_right_of, near) find elements by their boxes on screen; use them sparingly because layouts move.
  • switch_to.new_window opens a tab or window and switches to it; element screenshots and window rectangle calls round out the API.
  • DevTools access and WebDriver BiDi bring network, console and emulation features; support depends on browser and version.

Next: DevTools Protocol and BiDi