Exploring Selenium 4 Alpha: DevTools, Window Management, Relative Locators & Full‑Page Screenshots

This article demonstrates Selenium 4 Alpha's new capabilities—including easier DevTools access via ChromiumDriver, independent window and tab handling, enriched relative locators, and Firefox full‑page screenshot support—by providing clear code examples and practical explanations for Java WebDriver users.

FunTester
FunTester
FunTester
Exploring Selenium 4 Alpha: DevTools, Window Management, Relative Locators & Full‑Page Screenshots

ChromiumDriver and DevTools

Selenium 4 introduces a unified ChromiumDriver class that gives direct access to browser DevTools, replacing the previous separate EdgeDriver and ChromeDriver implementations. The following Java snippet creates a driver, opens a session, and runs a JavaScript console log through DevTools.

// create driver
WebDriver driver = new ChromeDriver();
Connection connection = null;
DevTools devtools = new DevTools(connection);
devtools.createSession();
String message = "chrome 浏览器测试Demo!";
driver.get("https://www.bing.cn");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("console.log('" + message + " ' ) ;");

The code demonstrates how DevTools can be used for tasks such as performance evaluation and page‑load timing.

Better Window and Tab Management

Selenium 4 adds the ability to open and control multiple windows or tabs without relying on a separate driver instance. This is especially useful when tests need to interact with different pages simultaneously.

WebDriver window2 = driver.switchTo().newWindow(WindowType.TAB);
window2.get("url");
WebDriver window1 = driver.switchTo().newWindow(WindowType.WINDOW);
window1.get("url");

The newWindow() method creates a new window or tab based on the WindowType argument.

Relative Locators

Selenium 4 Alpha enriches element location by allowing selectors relative to other elements. Available methods include: toLeftOf() – element positioned to the left of a reference element. toRightOf() – element positioned to the right of a reference element. above() – element located above a reference element. below() – element located below a reference element. near() – element within a configurable pixel distance (default 50 px) of a reference element.

Full‑Page Screenshot

Firefox now supports a dedicated method for capturing the entire page without casting to TakesScreenshot. The driver must be cast to FirefoxDriver to access this functionality.

File src = ((FirefoxDriver) driver).getFullPageScreenshotAs(OutputType.FILE);

These enhancements illustrate Selenium 4's focus on more powerful, flexible browser automation, encouraging developers to explore further capabilities.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DevToolsautomation testingSeleniumWebDriver
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.