Selenium 4.0 Released: New Features, Comparison with Previous Versions and More

Selenium 4.0 is officially released! It includes new features, improvements. I wanted to share with you some important updates on this release.

Selenium 4.0 Released: New Features, Comparison with Previous Versions and More

Selenium 4.0 is officially released! It includes new features, improvements. I wanted to share with you some important updates on this release. 

First of all, you need to upgrade your Selenium version to 4.0 and let’s start with upgrading. 


Before introducing the new features, I want to share how to upgrade your Selenium dependencies. The only thing you need to do is change your dependency version from 3.x.x to 4.0.0 - that’s it. If you are using Maven or Gradle, just change your version and install the new libraries. 


The first new feature I want to mention is simplified capability setting. In the older versions of Selenium, you had to set all capabilities in the DesiredCapabilities object for setting remote driver capabilities. In 4.0, you can set them with options directly. So you don’t need to define capabilities individually.

Here is the older usage of capabilities;


And here is the new way of using those capabilities;



Before Selenium 4.0, you had to send two parameters to use a wait: time and type of time. But now you can use the Duration class and types of this class directly. 

Here is the old way of declaring waits;


This usage is deprecated in 4.0; here is the new declaration format:


Here is a list of all durations supported by Duration.


All of these are accepted as long variable types. Keep in mind that you should import java.time.Duration; and not any other Duration class.

On the other hand, you will be able to set wait times in the browser options section. Generally, we set wait time in the hooks, but from now on there is a method in the options for setting waits. 

Here are some examples;


Relative Locators

While writing automation scripts, finding a locator of an element can be painful. There can be multiple elements, finding the correct XPath might be challenging, the XPath itself might be complex. In the new feature of the Selenium, you can use relative locators to define web elements in relation to other elements, such as below, above, toLeftOf, toRightOf or near. It makes your code more readable and friendly. This feature is my favorite one in this release. 

You can think of it like writing a relative XPath but this feature makes it easier to find locators. This is because finding a unique XPath or CSS selector can be hard and you may want to use the simple relation between elements.

Selenium 4.0 uses the JavaScript function getBoundingClientRect() to locate these relative elements, therefore it will give correct results any time. 

Let’s have a look at the example below and see the differentiation of the Relative Locators feature.

Login Page

Login Page Sc.

Let’s try to get the label for the password in the above examples. As you can see, there are multiple labels on the page and the best way to locate this label is getting hold of the password input which has an ID and moving to the preceding sibling. Instead of writing a complex XPath, you can use the above method and get the requested element easily:


OutPut => Password



These methods have the same logic as above and below. Just define one unique element and move to the left or right side of this element. Let’s examine this example;


toLeftOf/toRightOf/neartoLeftOf/toRightOf/near Sc


Above is a table, and, most of the time, handling tables might be harmful. You should use indexes heavily or find the parent element with text and move the child or vice versa. But with relative elements, you can make this transition smoother. Just locate the requested cell and move left, right directly without caring about the relation between elements. 

Let’s click the delete button next to the given website name with the relative elements.


Or you can use the near method instead of the toRightOf. 


Notice how I didn’t check any relationships between elements - child, parent or any other. I have just used the above, below, right or left method directly.

For using those methods, there is no rule for having any relation between those elements. It will look above elements directly not inside of the DOM but on the screen. So this is the tricky part of these methods.

Opening new windows, tabs

For older versions of Selenium, if you want to open a new window or tab, you should have a new driver object and use the window handler method for using this object. With Selenium 4.0, you can open a new window, or tab with the switchTo method easily. Let's take a look at an example:. 

Navigate to the one website, open a new window, and navigate to the other website. 


Or you may want to open a new tab, so you need to change WindowType only;

DevTools protocol

With the Selenium 4.0 API, you can use Chrome devtools such as network or profiler. I will show you how to set your geolocation with devtools;


The code block above sets my geolocation to the given coordinates. It is useful for some cases, for instance, if the app requires it to be in a certain location.

Selenium Grid Improvements

The older version of the Selenium Grid was complicated and it was not easy to set up. But the new grid comes with built-in Docker support and you can easily run the grid in a container rather than preparing virtual machines. Besides, you will no longer need to set up nodes and the hub separately with Selenium 4.0. The new Grid architecture comes with three modes and you can use any of them;

  1. Standalone mode
  2. Fully Distributed mode
  3. Hub and Node

I have walked through the new features of Selenium Grid and gave some examples of the new features. I hope you will enjoy the new release. At the moment I am working on a new article for setting up Selenium Grid. Stay on the line and keep learning!

 Click Here to Discover All Selenium-Related Blogs Posts

Selcuk Temizsoy

Experienced Software Test Automation Engineer working in different industries. Software Test Consultant at kloia