Selenium 4 Features – 2021

Hi All, In today’s article we will look into some of the Selenium 4 features. Although, still in its Alpha release, Selenium 4’s stable release is expected to hit the market soon.

As of the date of this article, the latest Selenium Alpha release is v4.0.0-alpha-7Check Official ChangeLog File for the latest updates.

Selenium 4

There are changes in many areas in Selenium 4 like Selenium Grid, Selenium IDE and Selenium Webdriver, but we will be mainly focusing on changes in Selenium Webdriver.

Comparing Selenium3 vs Selenium4

Selenium 4Selenium 3
Can take both page & Web Element ScreenshotHad only option to take screenshot
Opening new tab/window on the browserHad no option to do so
Relative locators introducedUse ID, Name, ClassName, Tag, Xpath, CSS, LinkText, PartialLinkText for locating element
getRect() is introduced for fetching dimensions of web elementDimension and point classes were used for fetching dimension
Chrome Dev tools API introducedUser had to manually work with Chrome Dev Tool

A Closer Look Into Selenium 4 Code.

Capturing Screenshot of a section in a web page

 WebElement email = driver.findElement("email"));  
 File src = email.getScreenshotAs(OutputType.FILE);  
 FileUtils.copyFile(src,new File("File Location"));

Take Screenshot of Web Elements

WebElement followByEmail=driver.findElement("FollowByEmaill"));
File src = ((TakesScreenshot)followByEmail).getScreenshotAs(OutputType.FILE);  
 FileUtils.copyFile(src,new File("file location")); 

Opening a new tab on the browser.


Relative/Friendly Locators

WebElement nextButton = driver.findElement("nextButton"));  
WebElement previousButton = driver.findElement(withTagName("button").toLeftOf(nextButton));  

getRect() for finding dimensions and position.

WebElement loginbtn = driver.findElement("login"));  
Rectangle rect = loginBtn.getRect();  

Chrome Devtools API

public DevTools getDevTools(){  
return devTools.orElseThrow(() -> new WebDriverException("Unable to create Devtools Connection"));  

Some Frequently Ask Question

How to take screenshot of web element in Selenium 4?

Selenium 4, we can take a screenshot of web elements on a web page. Just grab the element by findElement() method and use getScreenshotAs() method. Suppose you want to take a screenshot of an email field on a webpage. Then the below code does it for you :

How to Capturing Screenshot of a section in Selenium 4?

When it comes to capturing a screenshot of a section of a web page and by section, I mean a table or a form on a web page, we can locate the web element(again a table or a form or a div) and use getScreenshotAs() method on it.

How to opening a new tab on the browser in Selenium 4?

Selenium 4 has come up with many features and one being, opening a new tab on the browser(bank tab).   For opening a new blank window newWindow() method is used in Selenium.

How to find Relative/Friendly Locators?

Locators play an important role in finding/locating elements on a web page. Below are the methods that are used for relatively locating elements in Selenium 4 :
below(): finds element which is below the already located/searched element.
toLeftOf() : finds element which is in the left of the already located/searched element.
toRightOf() : finds element which is in the right of the already located/searched element.
above(): finds element which is above the already located/searched element.
near(): finds element which is at most 50px away from located/searched element.

How to use getRect() for finding dimensions and position?

In Selenium 4, getRect() method is used to get the dimensions of a web element like height, width, X-Coordinate, Y-Coordinate.   As told in the previous section, getRect() also uses getBoundingClientRect() function of Javascript to get the dimensions of web elements.

Selenium Training Videos

Generate Meaningful Test Data For Selenium Automated Tests

Manage Locator in Optimize Way and Some

Call Now