New Tricks in Selenium 4

Sauce AI for Test Authoring: Move from purport to executing in minutes.|xBack to ResourcesBlogPosted

April 27, 2026 · 6 min read · Tool Comparison

Sauce AI for Test Authoring: Move from purport to executing in minutes.

|

x

Back to Resources

Blog

Posted July 28, 2023

New Tricks in Selenium 4

Learn what & # x27; s new in Selenium 4, including wind and tricks for making the most of this democratic test automation fabric.

Documentation

In my last posts, we talked about, and. If you & # x27; re ready to upgrade to Selenium 4, hither & # x27; s a guide on. Today, we ’ ll cover some details about the new tricks and capabilities that Selenium 4 offers.

Finding Elements and Relative Locators

Finding elements on a page can be hard. I & # x27; ve seen loads of people expend rarifyXPathexpressions, and prove to estimate out complex CSS chooser and thing like that. There have be unharmed talks about the subject at SeleniumConf. Surely there must be a best way to do this?

Think about how we describe where an element is on the page. Think about how you ’ d do this over the sound. You ’ d ne'er talk about the raw DOM, “ Ah, find the 5th DIV element nested inside the SPAN with an ‘ id ’ of ‘ foo. ’ ” You ’ d just ne'er say that! Instead, you ’ d say something like, “ Just find that thing above that persona, and to the right of that connection, ” when talking about where things are located on the page.

A long clip ago, a projection phoneSahistarted to locate elements like this. Sahi hadRelation APIs, which are a endearing way of finding elements, and it was very, very fluent and pleasant to use. When I was commence WebDriver days ago, I was talking withNarayan(creator of Sahi) and I promised him that I was going to make off apply this flagship feature of Sahi. It was something that was lovely, but it was too something Sahi was justly proud of. However, there are now other tools, such as Tyco, that are implementing this style of API, so it seems like the time is correct to do the like in Selenium. In Selenium 4 we call them “ relative locators. ” You may sometimes see us concern to them as “ well-disposed locators, ” since the initial implementation ring them that, but “ relative ” better describes how they work. We have a handful of them: near, above, below, left of, right of. They allow you to talk in human language about where an element is on a page.

Looking to the futurity, we & # x27; re likewise planning to enable users to add new types of locators, not only on the on the guest side, but also to the Selenium Server. For example, although JS UI frameworks come and go (anyone else remember using jquery?), it ’ d be nice to be able to add a custom locater for the current tools people like, such as React.

Learn more about finding elements and relative locators in Selenium 4:

Authenticating to Web Sites

One feature that people have been asking for since we started the project has been the ability to authenticate to a website. Previously, you could do this by crafting the URL the browser proceed to properly, but this leaks credentials to any man-in-the-middle and leaves them in server logs, so browsers get slowly removed this part of functionality. That ’ s unfortunate, since it ’ s something that we know citizenry frequently need to do in their tests. In Selenium 4, we now offer a mechanics to register a username and word that can be utilize to authenticate against these sites.

Intercepting Network Traffic

A common complaint ofSeleniumtests is that they ’ re slow and flakey. While the bindings to the browser are first-class, and fully described by the W3C WebDriver specification, it is true that any end-to-end tryout is likely to suffer more flakiness than a unproblematic unit test—there are just more moving constituent, and more possibilities for thing to go wrong.

One way to resolve this issue is to stub out the backend of the application, stop meshing traffic in the test and revert pre-canned responses. Tools such ascharlatanmake this leisurely for API testing. Wouldn ’ t it be nice if there was a similar puppet for Selenium?

With Selenium 4, we now provide a mechanism to do this, using the NetworkInterceptor (well, that ’ s what we call it in the Java bindings). Pass it your WebDriver instance, and it ’ ll be call every clip the browser is about to create an HTTP request, permit you to render near anything you want.

Learn more about.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

The Chrome Debugging Protocol

As I mentioned previously, one of the nice things about Selenium 4 is our work to ensure a stable and modern user-facing API. What perform this mean? To start, we need to acknowledge that after a long time there ’ s now competition back in the browser automation space. Notably in the form of Puppeteer and Cypress, and take a step back, what do they offer? They build upon theChrome DevTools Protocol (CDP), which is a protocol germinate to enable a debugger inside Chromium-based browsers.

Because of what it was contrive to do, it is a chatty protocol, and not a user-facing API that allows you to introspect into the browser. Worsened, introduce a network hop between the test and the browser, this chattiness leads to slower test as the network latency gain. That is why instrument such asPuppeteerwant you to run on the local machine. That ’ s great for the hurrying of an individual tryout, but makesparallelizationharder, as you can & # x27; t use service such as Sauce Labs easily.

To complicate matters, because the CDP is designed as adebuggingprotocol, it can change with no notice between edition. That ’ s why Puppeteer andCypressare tie to specific versions of browser, and that causes a dilemma for you as a test author: how do you try on multiple versions of a browser? Updating the examination API to get a different browser can lead to you no longer making valid API calls. Not doing so ties you to a potentially outdated version of a browser.

Despite this, employ the CDP opens up a host of possibilities, and that ’ s why we ’ ve added support for it in Selenium 4. In fact, some of our new features are built on top of it (though we hide the details!)

But we ’ ve also proceed further than just bestow introductory support. Selenium 4 can support multiple versions of the CDP at the same time, meaning you can examine with both the current and beta versions of Chromium-based browsers without needing to rewrite tests. We do this by providing an “ idealized CDP, ” which is stable and extend the feature we feel tests demand. If that idealized view of the world isn ’ t decent for you, we also display the raw CDP APIs too, giving you the tractability to choose what ’ s best for your tests.

We ’ re feeding this experience into the new W3C WebDriver Bidi specification, which is facilitate make that a better fit for testers.

Learn more about.

I am leaving out of this post all the details connect to another crucial trick in Selenium 4, the newSelenium Grid.

Ready to start testing with Selenium and Sauce Labs?

Let & # x27; s go!

LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered stylemark owned by Sauce Labs Inc. in the United States, EU, and may be registered in early jurisdiction.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free