QA Interview Hub · Knowledge base · Quizzes · Українська

Selenium + Java › WebDriver: architecture and drivers

How Selenium WebDriver works: client, protocol, driver, browser

Test code calls the Selenium library, which sends commands over the W3C WebDriver protocol to the browser's driver, and the driver controls the browser. Commands are HTTP requests with JSON within a session, so every action costs a network round trip. The close method closes only a window, while quit ends the session and stops the driver process, so it is called in @AfterEach or finally.

Selenium · WebDriver

After a run, forty chromedriver processes sit in the task manager. Where did they come from?

A nightly CI run finished green and the next one failed: the agent ran out of memory. The process list shows dozens of chromedriver and chrome processes nobody stopped. The tests called driver.close(), and it seemed the browser was closed. To see why it was not, you need to know the parts Selenium consists of and what lives between the line driver.get(...) and the page in the browser. This lesson is about the chain client, protocol, driver, browser, about the session, and about why this design has consequences for test stability and speed.

How it works and why. Selenium WebDriver does not control the browser directly. The test code calls a library (selenium-java), which turns every call into a command of the W3C WebDriver protocol. The protocol describes commands as HTTP requests with a JSON body. The commands are received by the driver: a separate executable for each browser (chromedriver, geckodriver, msedgedriver; for Safari the driver ships with the OS). The driver already knows how to control its browser: in Chrome through its internal protocol, in Firefox through its own. That is how a test written once works in different browsers: a standardised layer stands between them.

The path of one click

Test (Java)driver.findElement(By.id("pay")).click()selenium-java clientturns a call into a commandHTTP + JSON, W3C standardPOST /session/{id}/elementDriver (chromedriver…)a separate process, its own portBrowserperforms the action, returns the result
Figure 1. Every API call goes through the whole chain and waits for the reply. The driver is a separate process, and it is the one that stays if nobody stops it.

The whole exchange happens inside a session. The new ChromeDriver() constructor starts the driver process, sends POST /session with the wanted capabilities and receives a session id. Every later command carries that id in its address. The main commands (shortened):

POST   /session                       → create a session, id in the reply
POST   /session/{id}/url              → driver.get(url)
POST   /session/{id}/element          → findElement(by)
POST   /session/{id}/element/{eid}/click
GET    /session/{id}/title            → getTitle()
DELETE /session/{id}                  → quit()

Finding an element sends a body with a strategy and a value:

{ "using": "css selector", "value": "[data-testid='pay']" }

The reply carries not the element itself but a reference to it (an element reference, a unique string). The remaining commands on that element (click, sendKeys) are addressed to that reference. That is also where StaleElementReferenceException comes from: the reference lives as long as the node lives in the DOM (details in the lesson “StaleElementReferenceException and other common interaction exceptions”).

Example: from hanging processes to a clean shutdown. The starting code that leaves a process behind every time:

class CheckoutTest {
    WebDriver driver;

    @BeforeEach
    void start() {
        driver = new ChromeDriver();
    }

    @AfterEach
    void stop() {
        driver.close(); // closes only the window
    }
}

close() closes the current window. When there is a single open window, the browser usually disappears, which gives the impression that everything is fine. But the chromedriver process keeps running: under the W3C standard the session ends with the last window, yet only quit() stops the driver itself. It is most visible when a test fails in the middle of a scenario, when a fixture throws, or when several windows are open: close() closes only one. quit() does everything needed: ends the session (DELETE /session/{id}), closes all windows and stops the driver process.

@AfterEach
void stop() {
    if (driver != null) {
        driver.quit();
    }
}

The null check is needed because if the driver constructor threw (for example, a session could not be created), the field is empty, and an exception from @AfterEach would mask the real cause. Closing belongs in @AfterEach or finally, not at the end of a test: then the shutdown also runs after a failure.

Limits and alternatives. The request-response model has consequences. Every action is a network round trip: on Selenium Grid or in a cloud where the driver is far away, hundreds of small calls (read text, an attribute, a state) add up to noticeable time, so redundant element reads are worth cutting. The browser cannot tell the test about an event (a console error, a network request): the test only asks. For events there is the two-way WebDriver BiDi protocol, covered in the lesson “BiDi instead of CDP, Actions, files, iframes and windows”. Selenium 4 speaks only the W3C protocol: the old JSON Wire Protocol and Selenium RC are no longer options. Playwright and Cypress are built differently: the first keeps a persistent connection to the browser, the second runs inside it, so automatic waits are built in there, while in Selenium you have to describe them explicitly.

Common mistakes.

  • close() instead of quit() in the teardown: driver processes pile up and exhaust the agent's memory.
  • Creating the driver in a static field: it cannot be closed after each test, and the session lives longer than needed.
  • Calling quit() twice or any command after it: you get NoSuchSessionException, because the session no longer exists.
  • Assuming Selenium "sees" everything the user sees: it works through the DOM and the protocol, and OS dialogs (file choosers, printing) are not available to it.

Check yourself

A test calls driver.close() at the end, and a chromedriver process stays in the system. Why, and what should change?

Answer

close() closes only the current window; even when it is the last one and the session ends, the driver process stays. quit() sends DELETE /session/{id}, closes all windows and stops the driver process. It is called in @AfterEach or finally so that it also runs after a failure.

Why are three calls for the text, the class and an attribute of one element slower on a remote Grid than locally, although the code is identical?

Answer

Each call is a separate HTTP request to the driver (on Grid also through the router to the node), so network latency multiplies by the number of calls. That is why redundant reads from the page are cut, and complex browser-side logic is sometimes run as one script.

In an interview. Junior: The test calls the Selenium library, it sends commands to the browser driver over the W3C WebDriver protocol, and the driver controls the browser. I end the session with quit(), while close() closes only a window. Middle/Senior: Commands are HTTP requests with JSON to a separate driver process, so each action costs a network round trip, and there are no browser events until BiDi is connected. I create the driver per test and close it in a fixture even after a failure, so processes do not pile up on CI and sessions do not hang on Grid.

More articles in this chapter