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-7 – Check Official ChangeLog File for the latest updates.

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 4 | Selenium 3 |
Can take both page & Web Element Screenshot | Had only option to take screenshot |
Opening new tab/window on the browser | Had no option to do so |
Relative locators introduced | Use ID, Name, ClassName, Tag, Xpath, CSS, LinkText, PartialLinkText for locating element |
getRect() is introduced for fetching dimensions of web element | Dimension and point classes were used for fetching dimension |
Chrome Dev tools API introduced | User 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(By.id("email"));
File src = email.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src,new File("File Location"));
Take Screenshot of Web Elements
WebElement followByEmail=driver.findElement(By.id("FollowByEmaill"));
File src = ((TakesScreenshot)followByEmail).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src,new File("file location"));
Opening a new tab on the browser.
driver.get("https://www.google.com");
driver.switchTo().newWindow(WindowType.WINDOW);
driver.navidate().to("https://www.meetahsan.com");
Relative/Friendly Locators
WebElement nextButton = driver.findElement(By.id("nextButton"));
WebElement previousButton = driver.findElement(withTagName("button").toLeftOf(nextButton));
getRect() for finding dimensions and position.
WebElement loginbtn = driver.findElement(By.id("login"));
Rectangle rect = loginBtn.getRect();
System.out.println(rect.getHeight());
System.out.println(rect.getWidth());
System.out.println(rect.getX());
System.out.println(rect.getY())
Chrome Devtools API
public DevTools getDevTools(){
return devTools.orElseThrow(() -> new WebDriverException("Unable to create Devtools Connection"));
}
Some Frequently Ask Question
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 :
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.
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.
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.
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