Introduction
Selenium 4 is the latest major release of Selenium and introduces several powerful features that make browser automation more stable, efficient, and easier to develop. It brings significant improvements over Selenium 3, including better browser communication through the W3C WebDriver Protocol, automatic browser driver management using Selenium Manager, support for Relative Locators, integration with Chrome DevTools Protocol (CDP), improved Selenium Grid, and many API enhancements.
These new features simplify test automation, reduce setup complexity, improve cross-browser compatibility, and provide better support for modern web applications.
In this tutorial, you’ll learn the major features introduced in Selenium 4, practical examples, real-world use cases, best practices, and why upgrading to Selenium 4 is recommended.
What is Selenium 4?
Selenium 4 is the fourth major version of the Selenium automation framework. It builds upon Selenium 3 by introducing modern browser automation capabilities while maintaining backward compatibility for most existing Selenium scripts.
Selenium 4 follows the W3C WebDriver Standard, making browser automation more consistent and reliable across different browsers.
Major Features of Selenium 4
Selenium 4 introduces several new features that improve automation development.
Some of the most important features include:
W3C WebDriver Standard
Selenium Manager
Relative Locators
Chrome DevTools Protocol (CDP) Support
Improved Selenium Grid
Better Window and Tab Management
New WebElement Methods
Enhanced Actions API
Improved Screenshot Support
Better Documentation and API Improvements
Let’s understand each feature in detail.
W3C WebDriver Standard
One of the biggest improvements in Selenium 4 is its complete adoption of the W3C WebDriver Protocol.
Earlier versions of Selenium used browser-specific communication methods, which sometimes caused compatibility issues.
With Selenium 4:
Browser communication is standardized.
Better cross-browser compatibility.
More reliable automation.
Faster browser interaction.
This standard is supported by all major browsers.
Selenium Manager
Selenium 4.6 introduced Selenium Manager, which automatically manages browser drivers.
Earlier, users had to:
Download ChromeDriver manually.
Keep browser drivers updated.
Configure driver paths.
With Selenium Manager, Selenium automatically:
Detects the installed browser.
Downloads the correct browser driver.
Configures it automatically.
Example:
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
driver.quit()
No manual ChromeDriver download is required in most cases.
Relative Locators
Selenium 4 introduces Relative Locators, allowing elements to be located based on their position relative to other elements.
Available relative locators include:
above()
below()
to_left_of()
to_right_of()
near()
Example:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.relative_locator import locate_with
driver = webdriver.Chrome()
password = driver.find_element(
locate_with(By.TAG_NAME, "input").below(
driver.find_element(By.ID, "username")
)
)
Relative locators make complex page layouts easier to automate.
Chrome DevTools Protocol (CDP) Support
Selenium 4 provides built-in support for the Chrome DevTools Protocol.
Using CDP, automation engineers can:
Capture browser console logs.
Monitor network requests.
Block network calls.
Simulate slow network conditions.
Emulate mobile devices.
Override geolocation.
Capture performance metrics.
This is especially useful for advanced automation and debugging.
Improved Selenium Grid
Selenium Grid has been completely redesigned in Selenium 4.
New improvements include:
Easier installation
Simplified configuration
Better scalability
Improved performance
Enhanced monitoring
Docker-friendly architecture
Selenium Grid now supports distributed execution more efficiently than previous versions.
Better Window and Tab Management
Managing multiple browser windows and tabs is much easier in Selenium 4.
Example:
driver.switch_to.new_window("tab")
Open a new browser window:
driver.switch_to.new_window("window")
This eliminates the need for complex JavaScript-based solutions.
New WebElement Methods
Selenium 4 introduces additional methods for working with web elements.
Example:
Retrieve a DOM attribute:
element.get_dom_attribute("class")
Retrieve a CSS property:
element.value_of_css_property("color")
These methods provide better access to element properties and styles.
Enhanced Actions API
The Actions API has been improved to support more complex user interactions.
Examples include:
Mouse hover
Double click
Right click
Drag and drop
Keyboard shortcuts
Multiple keyboard actions
These improvements provide better support for modern web applications.
Improved Screenshot Support
Selenium 4 allows capturing screenshots of:
Entire browser window
Individual web elements
Example:
element = driver.find_element(By.ID, "logo")
element.screenshot("logo.png")
Capturing element-level screenshots is useful for debugging and visual validation.
Improved API Consistency
Several deprecated methods from Selenium 3 have been removed or updated.
For example:
Older style:
driver.find_element_by_id("username")
Recommended Selenium 4 style:
from selenium.webdriver.common.by import By
driver.find_element(By.ID, "username")
Using the new locator syntax improves code readability and aligns with current Selenium standards.
Practical Example
The following Selenium 4 script launches Chrome, opens a website, prints the page title, and closes the browser.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
print(driver.title)
driver.quit()
Output
Example Domain
With Selenium Manager, no manual driver setup is required in most environments.
Selenium 3 vs Selenium 4
| Feature | Selenium 3 | Selenium 4 |
|---|---|---|
| W3C WebDriver Standard | Partial Support | Full Support |
| Selenium Manager | No | Yes |
| Relative Locators | No | Yes |
| Chrome DevTools Protocol | Limited | Yes |
| Improved Selenium Grid | No | Yes |
| New Window/Tab API | No | Yes |
| Element Screenshots | Limited | Yes |
| Modern Locator Syntax | No | Yes |
Real-World Use Cases
Selenium 4 is widely used for:
Cross-browser web automation
Continuous Integration (CI/CD)
Parallel test execution
Responsive web testing
Performance monitoring
Network interception
Browser debugging
Enterprise automation frameworks
Common Mistakes Beginners Make
Using Deprecated Selenium 3 Methods
Instead of:
driver.find_element_by_id("username")
Use:
driver.find_element(By.ID, "username")
Downloading Browser Drivers Manually
Modern Selenium versions include Selenium Manager, which can automatically handle browser drivers in most cases.
Ignoring Selenium 4 Features
Many beginners continue using older techniques without taking advantage of features like Relative Locators and improved window management.
Best Practices
Always use the latest stable version of Selenium.
Prefer Selenium Manager over manual browser driver management.
Use the modern
Bylocator syntax.Learn Relative Locators for complex page layouts.
Use Chrome DevTools Protocol for advanced browser debugging.
Upgrade existing Selenium 3 projects to Selenium 4 whenever possible.
Frequently Asked Questions (FAQs)
What is Selenium 4?
Selenium 4 is the latest major version of the Selenium automation framework that introduces new features, improved browser communication, and enhanced automation capabilities.
What is the biggest improvement in Selenium 4?
The adoption of the W3C WebDriver Protocol and the introduction of Selenium Manager are among the biggest improvements.
Do I still need to download ChromeDriver manually?
In most cases, no. Selenium Manager automatically downloads and manages compatible browser drivers.
What are Relative Locators?
Relative Locators allow you to locate elements based on their position relative to other elements, such as above, below, to the left, or to the right.
Should I upgrade from Selenium 3 to Selenium 4?
Yes. Selenium 4 offers better browser compatibility, modern APIs, improved performance, simplified driver management, and many new features.
Key Takeaways
Selenium 4 is the latest and most advanced version of the Selenium automation framework.
It fully supports the W3C WebDriver Standard for reliable cross-browser automation.
Selenium Manager simplifies browser driver management by handling downloads automatically.
Relative Locators make locating web elements easier in complex layouts.
Chrome DevTools Protocol support enables advanced browser debugging and testing.
Selenium Grid has been redesigned for better scalability and parallel execution.
Selenium 4 introduces modern APIs, improved window management, and enhanced WebElement methods.
Upgrading to Selenium 4 is recommended for building modern, maintainable automation frameworks.
