Browser automation with JavaScript is more powerful than ever. And if you're just starting out, using the selenium webdriver npm package is one of the most flexible ways to write and run your own automated browser scripts.
Whether you're testing a login flow or building a web scraper, Selenium WebDriver helps you take control of the browser automatically. Add in NPM, and now you have a fast setup, easy dependency management and the full support of the Node.js ecosystem.
In this guide, we'll walk you through everything you need to know to get started with the selenium-webdriver package using NPM:
Let’s get into it.
Selenium WebDriver is an open-source API and protocol for automating browsers. It gives you programmatic control over the browser window, allowing you to run automated actions without manual clicks or keyboard input.
At its core, the architecture is simple:
Think of Selenium WebDriver like a remote control for your browser. You press play, it opens the site. You press pause, it checks a value. You can even switch between tabs or windows without ever touching your mouse.
NPM stands for Node Package Manager. It is the default package manager that comes with Node.js. Developers use it to install libraries, manage dependencies, and share reusable code across projects.
There are two key parts to NPM:
NPM works like an app store for your project, where you don’t have to build every feature from scratch. Basically you just search for the module you need, add it, and it’s ready to use. This saves time and keeps your project lean.
When you install a package, it gets listed in a file called package.json. This file tracks your project’s dependencies. It helps you install the same versions across different machines, environments, or teams. It also supports scripts, version control, and metadata.
The selenium-webdriver npm package gives JavaScript developers full access to this control layer. It works with Node.js, making it ideal for teams already building with JavaScript. You can write async functions, run scripts locally, or plug it into a CI/CD pipeline.
Other supported languages include Python, Java, C#, and Ruby. But if you're using NPM to manage your automation stack, JavaScript is the best entry point.
With the selenium webdriver npm package, you get low-level browser automation with the flexibility of the Node.js ecosystem.
In selenium webdriver npm projects, NPM plays a critical role. It pulls in the WebDriver bindings, keeps them updated, and manages related tools like browser drivers or test runners. That means smoother setup, cleaner updates, and less manual configuration.
Once you get familiar with NPM, you can use it to build, test, and maintain browser automation projects with confidence and speed.
Before you install the selenium webdriver npm package, make sure your system is ready. These pre-requisites help ensure that your setup goes smoothly and your scripts run as expected.
These tools work together. Node.js and NPM handle the runtime and package management. The browser and its driver create the automation environment. Your JavaScript code brings them all to life using the selenium-webdriver API.
Once everything is in place, you're ready to start installing and writing your first test.
Now that the setup is ready, let’s install the selenium-webdriver package using NPM. These steps will help you create a clean environment and start writing browser automation scripts right away.
Step 1: Initialize a Project
Create a new folder for your project. Then open your terminal inside that folder and run:
npm init -y
This will generate a package.json file to track your dependencies.
Step 2: Install Selenium WebDriver Package
To add the core library, run:
npm install selenium-webdriver
You can pin a specific version if you want to lock your environment for better compatibility.
Step 3: Install Browser Driver
To automate Chrome, install ChromeDriver as a local dependency:
npm install chromedriver
You can also download it manually from the ChromeDriver site and update your system PATH if preferred.
Step 4: Verify Installation
Check your node_modules folder to confirm the packages are installed. You can also test a sample script or import the selenium-webdriver library in a JavaScript file to validate everything works.
For most selenium webdriver npm projects, using local installations is the safest option. It keeps the environment self-contained and repeatable across machines.
Once your dependencies are installed, it’s time to write your first selenium webdriver npm script. This will help you understand how browser automation flows from start to finish.
Start by creating a new file named test.js. This is where your automation logic will live. In this example, we will launch a browser, go to a website, find an element, perform an action, and then close the browser.
The following script uses async/await to handle asynchronous browser commands in a readable way.
const { Builder, By, until } = require('selenium-webdriver');
(async function runTest() {
const driver = await new Builder().forBrowser('chrome').build();
try {
await driver.get('https://katalon.com');
const element = await driver.findElement(By.name('q'));
await element.sendKeys('automated testing');
await driver.sleep(2000);
} finally {
await driver.quit();
}
})();
This script opens Chrome, navigates to Katalon’s homepage, searches for a phrase, waits briefly, and then closes the browser.
Each command in Selenium returns a promise. That’s why we use await before each action. It ensures the browser finishes one step before moving to the next.
When using the selenium-webdriver library with NPM, always remember to quit the browser at the end. This frees up system resources and keeps test runs clean.
For more flexibility, you can store the target URL in an environment variable. You can also add test frameworks like Mocha or Jest to write assertions, structure test cases, and integrate with CI pipelines.
With this basic script in place, you're ready to explore more advanced flows using the selenium webdriver npm setup.
Using the selenium webdriver npm package is one of the simplest ways to start browser automation in JavaScript. It gives you control, flexibility, and access to a well-supported ecosystem.
With this setup, JavaScript developers gain a clean, maintainable, and powerful way to automate real browsers. It’s a smart investment for any test workflow built with Node.js.