keepcalculator

06 Numbers and Powers

Selenium 4 Webdriver Architecture: Practical Guide

Card No. 06·70583 · Updated · Standard formula, shown below

Selenium 4 WebDriver architecture is a cleaner, more modular design than Selenium 3, with the W3C WebDriver protocol as its core. This guide explains what changed, how the pieces fit together, and how to apply it in real test automation projects. You’ll learn the key components, how to choose a driver, common pitfalls, and what the future holds.

Background: Why Selenium 4 Changed the Architecture

For years, Selenium 3 relied on a JSON Wire Protocol that was not an official standard. Every browser had its own driver, and the protocol was a de facto specification that sometimes caused inconsistencies. The W3C (World Wide Web Consortium) standardized WebDriver in 2018, and Selenium 4 adopted it fully. This means the communication between your test code and the browser is now standardized, making tests more reliable and reducing the need for browser-specific workarounds. The architecture also simplified the way Selenium Grid works, making it easier to run tests in parallel across multiple machines and browsers.

Core Components of Selenium 4 WebDriver

Understanding the architecture starts with knowing the four main pieces:

  • Client Libraries: The language bindings (Java, Python, C#, JavaScript, Ruby, Kotlin) that you write your tests in. They send commands to the WebDriver protocol.
  • WebDriver Protocol: The standardized REST API that defines how commands are encoded and transmitted. In Selenium 4, this is the W3C WebDriver protocol, which is now the only protocol used.
  • Browser Drivers: Executable files (like chromedriver, geckodriver, msedgedriver) that listen for commands from the client and translate them into actions the browser understands. Each browser has its own driver.
  • Browser: The actual browser (Chrome, Firefox, Edge, Safari) that executes the commands and returns results.

In Selenium 3, the client library would send commands to the driver using the JSON Wire Protocol, and the driver would then forward them to the browser. Selenium 4 eliminated the JSON Wire Protocol entirely, so the client talks directly to the driver using the W3C protocol. This reduces overhead and makes the whole process more efficient.

How Selenium 4 Architecture Works in Practice

When you run a Selenium 4 test, here’s what happens step by step:

  1. Your test creates a WebDriver instance, for example new ChromeDriver() in Java or webdriver.Chrome() in Python.
  2. The client library locates the browser driver (e.g., chromedriver) either from the system PATH or via the Selenium Manager (built into Selenium 4.6+).
  3. The client starts the driver executable as a separate process.
  4. The client sends an HTTP request to the driver, asking it to start a new browser session. This request uses the W3C WebDriver protocol.
  5. The driver starts the browser with the desired capabilities (like headless mode, browser version, etc.) and returns a session ID.
  6. All subsequent commands (find element, click, type, etc.) are sent as HTTP requests to the driver, which translates them into browser-specific commands using the browser’s automation interface (e.g., Chrome DevTools Protocol for Chrome).
  7. The browser executes the action and sends back the result (e.g., element found, text, screenshot).
  8. When you call driver.quit(), the client tells the driver to close the session, and the driver closes the browser and terminates itself.

This architecture is more direct than Selenium 3 because there is no intermediary JSON Wire Protocol translation. The commands are sent as JSON over HTTP, but the format is now standardized, so there are fewer bugs and inconsistencies.

Choosing the Right Driver and Setup

To use Selenium 4, you need the correct driver for each browser. Here are the common ones:

  • Chrome: chromedriver (managed by Selenium Manager automatically)
  • Firefox: geckodriver
  • Edge: msedgedriver
  • Safari: safaridriver (built into macOS, no separate download)

Since Selenium 4.6, Selenium Manager is built into the client libraries. It automatically downloads the correct driver version for your browser when you start a session, so you don’t have to manually download and manage drivers. This is a huge improvement over Selenium 3, where you had to manually match driver versions to browser versions. If you are using an older Selenium 4 version, you can still manually set the driver path using system properties (e.g., webdriver.chrome.driver).

Common Problems and How to Solve Them

Even with a cleaner architecture, you may encounter issues. Here are the most common ones and their fixes:

  • Driver version mismatch: The driver and browser versions must be compatible. Selenium Manager usually handles this, but if you manually set the driver, you’ll get a “session not created” error. Solution: update the driver or browser.
  • Port already in use: The driver starts on a specific port (default 9515 for chromedriver). If that port is busy, you’ll get an error. Solution: kill the process using the port or specify a different port with --port argument when starting the driver manually.
  • Element not found: This is often due to timing issues. Selenium 4 has built-in waits, but you should use explicit waits (e.g., WebDriverWait) to handle dynamic content.
  • Browser not launching in headless mode: Make sure you set the correct options. For Chrome, use --headless=new (the old --headless is deprecated).
  • Session not created exception: This usually means the driver couldn’t start the browser. Check that the browser is installed, the driver is correct, and the path is accessible.

Selenium Grid and Parallel Testing

Selenium 4 Grid has a new architecture that is more flexible and easier to set up. The old Grid had a hub and nodes; the new Grid uses a router, distributor, session map, and node components. You can run a standalone Grid with a single command: java -jar selenium-server-4.0.0.jar standalone. Then you can connect your tests to http://localhost:4444. For parallel testing, you can start multiple nodes with different browsers. The key benefit is that you can scale horizontally by adding more nodes, and the Grid automatically handles session distribution. This is ideal for running large test suites in CI/CD pipelines.

Comparison with Selenium 3 and Other Tools

Compared to Selenium 3, Selenium 4 offers:

  • W3C compliance: More stable and consistent behavior.
  • Better logging: Improved logging and debugging.
  • Relative locators: New ways to find elements (e.g., above, below, near).
  • Improved window and tab management: New APIs for opening new tabs and windows.
  • CDP support: Direct access to Chrome DevTools Protocol for advanced browser interactions.

When comparing to other tools like Playwright or Cypress, Selenium 4 still has the advantage of supporting multiple browsers (including Safari) and multiple languages. Playwright and Cypress are faster for some tasks because they use a different protocol, but Selenium remains the most widely adopted and flexible option.

Frequently Asked Questions

Do I need to download a driver for Selenium 4?

No, if you are using Selenium 4.6 or later, Selenium Manager automatically downloads the correct driver for you. For older versions, you need to download the driver manually and set its path.

What is the difference between W3C WebDriver and JSON Wire Protocol?

The JSON Wire Protocol was a preliminary specification used in Selenium 3. The W3C WebDriver is the official standard, which is more precise and eliminates many ambiguities. Selenium 4 uses only W3C, making tests more reliable.

Can I run Selenium 4 tests on Safari?

Yes, Safari is supported via safaridriver, which is built into macOS. You need to enable “Allow Remote Automation” in Safari’s Develop menu. Selenium 4 works with Safari 13 and later.

Why does my test fail with a “session not created” error?

This error typically means the driver could not start the browser. Common causes include driver version mismatch, browser not installed, or incorrect capabilities. Check the driver and browser versions, and ensure the browser path is correct.

Is Selenium 4 faster than Selenium 3?

In general, yes, because the W3C protocol reduces overhead and the code is more efficient. However, the actual speed depends on your test design and the browser. The biggest performance gains come from using parallel testing with Selenium Grid.

Can I use Selenium 4 with cloud testing services?

Yes, most cloud testing providers (like Sauce Labs, BrowserStack) support Selenium 4. You simply point your WebDriver to their remote URL and pass your credentials. The architecture is the same as using a local Grid.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →