In this post, we will cover the new features in selenium 4.
What is the difference between Selenium 3 and Selenium 4?
In Selenium 3, the JSON Wire Protocol is used to communicate between the test code and web browser.
ChromeDriver, GeckoDriver, etc., follow the W3C standard. This resulted in the encoding and decoding of requests as per the W3C protocol.
Selenium 4 uses the WebDriver W3C protocol. This eliminates the process of encoding and decoding that was necessary with JSON Wire Protocol in Selenium 3.
This change in architecture resulted in less flaky and more stable cross-browser tests.
How to Upgrade from Selenium 3 To Selenium 4
Most people are already the users of Selenium three with Page Object model (Selenium WebDriver) and would like to migrate from Selenium three to Selenium 4. Follow the under steps to improve from Selenium 3 to Selenium 4.
Using Selenium with Maven
If you are using Selenium with Maven, then it’s quite easy for you to upgrade to Selenium 4 by simply altering the version of Selenium to Selenium 4.
We can update the maven dependency to 4 like below:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
Let’s look at the feature that Selenium 4 offers

- Upgraded Selenium IDE
- Relative Locators in Selenium 4
- Improved Documentation
- Support for Chrome Debugging Protocol
- Multi Tab Opening
Upgraded Selenium IDE
- Selenium 4 IDE comes with a SIDE tool that allows the tester to run projects on a node.js platform.
- It provides capability to run cross-browser tests on local or Cloud Selenium Grid.
- Improved control flow mechanism that allows testers to write better condition statement like ‘while’ and ‘if’.
Relative Locators in Selenium 4
Selenium 4 brings a clean manner of finding factors with the inclusion of relative locators. This way testers can now discover net factors the usage of intuitive phrases which are frequently utilized by customers like:
- To left of : It is to locate a web element present on the left of a specified element.
- To right of: It is to locate a web element present on the right of a specified element.
- Above : It is to locate a web element just above the specified element.
- Below: It is to locate a web element just below the specified element.
- near(): It is to locate a web element at approx. 50 pixels away from a specified element. The distance can be passed as an argument to an overloaded method.
To left of Syntax
WebElement Email = driver.findElement(withTagName("input").toLeftOf(anylabelname));
To right of Syntax
WebElement Password= driver.findElement(withTagName("input").toRightOf(Email));
txtNameLabel.sendKeys("Password");
Below Syntax
WebElement seleniumTutorial = driver.findElement(withTagName("img").below(subject));
login.click();
Above Syntax
WebElement forgotpassword= driver.findElement(withTagName("span").above(subject));
forgotpassword.click();
Improved Documentation
Selenium 4 has been enhanced with better UI for documentation, which allows users to easily navigate to the desired section of the page and covers all information about tools and APIs that comes under the Selenium.
Support for Chrome Debugging Protocol(CDP)
- Selenium 4 provides support for Chrome DevTools Protocol (CDP) through the DevTools interface.
- We can utilize Chrome development properties like Fetch, Network, Profiler, Performance, Application cache, and more.
- QA teams can also leverage the APIs to simulate poor network conditions and perform geolocation testing.
- Testing under network condition like 3G, 4G and 5G can be performed with selenium 4.
- Emulating Geo Location
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
public void geoLocationTest(){
ChromeDriver driver = new ChromeDriver();
Map coordinates = new HashMap()
{{
put("latitude", 54.2372);
put("longitude", 0.2372);
put("accuracy", 1);
}};
driver.executeCdpCommand("Emulation.setGeolocationOverride", coordinates);
driver.get("www.swingli.com");
}
- Override Device Mode
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
public void deviceSimulationTest() {
ChromeDriver driver = (ChromeDriver) Driver.getDriver();
tools = driver.getDevTools();
tools.createSession();
Map deviceMetrics = new HashMap()
{{
put("width", 600);
put("height", 1000);
put("mobile", true);
put("deviceScaleFactor", 50);
}};
driver.executeCdpCommand("Emulation.setDeviceMetricsOverride", deviceMetrics);
driver.get("https://www.swingli.com");
}
- Listen to console.log events on a web page
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v87.log.Log;
public void consoleLogTest() {
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
devTools.send(Log.enable());
devTools.addListener(Log.entryAdded(),
logEntry -> {
System.out.println("log: "+logEntry.getText());
System.out.println("level: "+logEntry.getLevel());
});
}
Multi Tab Opening
In selenium 4, the user can open a URL in a new tab along with the existing tab without creating a web driver element.
driver.get("https://www.google.com/");
// Opens a new window and switches to new window
driver.switchTo().newWindow(WindowType.WINDOW);
// Opens Swingli homepage in the newly opened window
driver.navigate().to("https://www.swingli.com/");
Conclusion
In this article, we have covered the difference between selenium 3 and selenium 4, steps to upgrade to selenium 4, latest features of selenium 4. We hope Selenium 4.0 gives us the best experience and ability to do almost all the tasks which are unfulfilled in Selenium 3.