Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Selenium 4: What Changed for WebDriver Tests?

· 6 min read · Home Tech

In this article
  1. Background: Why Selenium 4 Matters
  2. Key Changes in Selenium 4 WebDriver
  3. Relative Locators: A Closer Look
  4. Window and Tab Management
  5. Selenium Grid: What Changed and Why It Matters
  6. Common Migration Issues and How to Fix Them
  7. Frequently Asked Questions
  8. Conclusion

Selenium 4, released in October 2021, is the first major overhaul of the Selenium project since 2018. For WebDriver tests, the most significant changes are the adoption of the W3C WebDriver protocol as the default and standard, a redesigned Selenium Grid with full Docker support, and the deprecation of the old JSON Wire Protocol. This article explains what those changes mean for your test code, your test infrastructure, and your day-to-day debugging.

Background: Why Selenium 4 Matters

Selenium WebDriver has been the de facto standard for browser automation for over a decade. The original protocol, JSON Wire Protocol (JWP), was a Selenium-specific REST API that required a translation layer between the client and the browser driver. In 2018, the W3C standardized the WebDriver protocol, and browser vendors began implementing it natively. Selenium 4 is the first version to fully embrace this W3C standard, dropping the JWP entirely.

This shift is not just cosmetic. It means that your Selenium 4 tests speak the same language as the browser driver (chromedriver, geckodriver, etc.) without any intermediary translation. The result is more stable sessions, fewer flaky errors, and a clearer separation of responsibilities. For testers, the practical outcome is that some old code patterns no longer work, but new features make tests more expressive and easier to maintain.

Key Changes in Selenium 4 WebDriver

Here are the most impactful changes for anyone writing or maintaining WebDriver tests:

  • W3C WebDriver Protocol Only: The JSON Wire Protocol is gone. All commands are now sent via the W3C WebDriver protocol. This means that if you were relying on JWP-specific capabilities or behaviors, you need to update them.
  • Relative Locators: A new way to locate elements based on their position relative to other elements. For example, driver.findElement(withTagName("input").above(By.id("password"))) finds an input above the password field. This reduces the need for brittle XPath.
  • Improved Window and Tab Management: New APIs for opening new tabs or windows and switching between them without the old driver.switchTo().window() dance. You can now do driver.switchTo().newWindow(WindowType.TAB).
  • Enhanced Selenium Grid: The Grid has been rewritten to support Docker, Kubernetes, and distributed testing out of the box. It is now easier to run tests in parallel across multiple machines and browsers.
  • Better Logging and Debugging: Selenium 4 includes a built-in RemoteWebDriver logging system that can help you diagnose issues with your WebDriver sessions.
  • Deprecations and Removals: Some old methods and classes have been deprecated or removed, such as Actions class methods that were not W3C-compliant, and the FindsBy interface.

Relative Locators: A Closer Look

Relative locators are one of the most talked-about features in Selenium 4. They allow you to find elements based on their spatial relationship to other elements. The available methods are above(), below(), toLeftOf(), toRightOf(), and near().

For example, consider a login form where the username field is above the password field. Instead of writing a fragile XPath like //input[@type='password'], you can write:

WebElement passwordField = driver.findElement(withTagName("input").below(By.id("username")));

This is more readable and less likely to break if the DOM structure changes. However, note that relative locators work by calculating the element's position on the page, so they may not work well with elements that are hidden or have zero size.

Window and Tab Management

In Selenium 3, opening a new tab required a JavaScript workaround or a complex sequence of window handles. Selenium 4 introduces a clean API:

driver.switchTo().newWindow(WindowType.TAB);

This opens a new tab and switches to it automatically. Similarly, WindowType.WINDOW opens a new browser window. This simplifies multi-tab testing scenarios, such as testing a "Sign in with Google" flow that opens a popup.

Selenium Grid: What Changed and Why It Matters

The Selenium Grid in version 4 is a complete rewrite. The old Grid was a monolithic server; the new one is modular, with separate components for the router, distributor, node, and session map. This architecture allows for better scaling and easier deployment.

For testers, the most visible change is the ability to run Grid nodes in Docker containers with a single command. For example:

docker run -d -p 4444:4444 --name selenium-hub selenium/hub:4.0

This makes it trivial to set up a local grid for parallel testing. The Grid also supports the WebDriver protocol natively, so you don't need to configure legacy protocol translation.

Common Migration Issues and How to Fix Them

If you are upgrading from Selenium 3, you may encounter these issues:

  • Capabilities that no longer work: Some browser-specific capabilities that were part of JWP are now ignored or cause errors. Use the W3C-compliant capabilities instead. For example, "browserName" and "browserVersion" are standard.
  • Actions class changes: The Actions class has been updated to follow the W3C specification. Some methods like clickAndHold() and release() still exist, but the underlying implementation is different. If you have complex action sequences, test them thoroughly.
  • Element screenshot and location: Some methods like getRect() have been added, while older methods like getLocation() and getSize() are still available but may be deprecated in future versions.

To migrate smoothly, start by updating your client library to the latest Selenium 4 version, then run your test suite and fix any errors. Most issues will be related to capabilities or the Actions class.

Frequently Asked Questions

Q: Do I need to change my test code when upgrading to Selenium 4?

Not necessarily. If your tests use standard WebDriver commands and avoid deprecated features, they will likely work without changes. However, if you used JWP-specific capabilities or the old Actions API, you will need to update those parts.

Q: Is Selenium 4 compatible with all browsers?

Yes, Selenium 4 supports all major browsers that implement the W3C WebDriver protocol, including Chrome, Firefox, Edge, and Safari. However, you need to use the correct driver executable (chromedriver, geckodriver, etc.) and keep them updated.

Q: What is the difference between Selenium 3 and Selenium 4 for parallel testing?

Selenium 4's Grid is more scalable and easier to set up, especially with Docker. The new Grid also supports the W3C protocol natively, which reduces the chance of protocol mismatch errors. For local parallel testing, you can use the built-in RemoteWebDriver with a local Grid.

Q: Will Selenium 4 slow down my tests?

No. The move to the W3C protocol eliminates the translation layer, which can actually improve performance slightly. The new features like relative locators may add a small overhead, but in practice, the difference is negligible.

Q: Can I still use Selenium 3 if I want to?

Yes, Selenium 3 is still maintained, but it is in maintenance mode. New features and browser updates will likely be added only to Selenium 4. It is recommended to upgrade eventually.

Q: What are the system requirements for Selenium 4?

Selenium 4 requires Java 8 or higher, Python 3.6 or higher, .NET Core 3.1 or higher, or Node.js 10 or higher, depending on the language binding. The browser drivers must be compatible with your browser version.

Conclusion

Selenium 4 represents a significant step forward for WebDriver tests. The adoption of the W3C protocol ensures better compatibility and stability, while new features like relative locators and improved window management make tests more readable and maintainable. The redesigned Grid simplifies parallel testing and containerization. While migration may require some adjustments, the long-term benefits are well worth the effort.