The Katalon Blog

Selenium WebDriver NPM Package: Setup and Usage

Written by Vincent N. | Sep 25, 2025 4:15:00 PM

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:

  • What Selenium WebDriver actually is and how it works
  • Why NPM makes a difference in JavaScript testing projects
  • What you need to install before using selenium webdriver via NPM
  • Step-by-step setup instructions with sample commands
  • How to write and run your first Selenium script using Node.js
  • Why NPM is the best way to manage Selenium WebDriver in JavaScript projects

Let’s get into it.

What is Selenium WebDriver?

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:

  • You write test code using WebDriver client libraries available in multiple languages. That code sends commands to a browser driver like ChromeDriver or GeckoDriver. The driver then talks to the browser using the W3C WebDriver standard or the older JSON Wire Protocol.
  • This creates a bridge between your script and the browser. You can open a URL, click a button, fill in a form, or check that a label is visible. It works just as well on Chrome as it does on Firefox or Edge.

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.

What is NPM?

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:

  • First, the npm CLI is the tool you run in your terminal. It lets you install packages, update them, and run commands.
  • Second, the npm registry is the online database that hosts all available packages, including the official selenium-webdriver package.

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.

What is selenium-webdriver npm?

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 Selenium WebDriver

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.

  • Node.js Installed: This allows you to run JavaScript outside of the browser. It is the foundation for using NPM and writing test scripts.
  • NPM Available: NPM is bundled with Node.js, but you should verify that it is installed and updated. It manages the selenium-webdriver package and other dependencies.
  • Browser Installed: You need at least one browser installed. Chrome, Firefox, and Edge are all supported by Selenium. Choose the one you want to automate.
  • Browser Driver: Each browser requires a matching driver. For Chrome, install ChromeDriver. You can download it manually or use an NPM package like chromedriver to simplify the process.
  • Basic JavaScript Knowledge: You should be comfortable writing asynchronous code. Selenium commands often use async functions and the await keyword to control browser flow.

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.

Steps to Install Selenium WebDriver with NPM

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:

Bash
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:

Bash
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:

 

Bash
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.

How to set up and run the Selenium Script

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.

JavaScript
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.

Why should you install and use Selenium WebDriver with NPM?

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.

  • Ease of setup is one clear advantage. A single command installs everything you need to begin. This helps you move quickly from idea to execution.
  • Cross-platform compatibility is another key benefit. Node.js and NPM work across Windows, macOS, and Linux. You can write your tests once and run them anywhere.
  • Integrated dependency management keeps your environment consistent. When you install the selenium-webdriver package, NPM records it in package.json. This file ensures that every team member or CI runner installs the same version.
  • Scalable automation is easy to achieve. You can extend your setup with libraries for test assertions, reporting tools, or integrations with CI platforms. The Node.js ecosystem gives you thousands of modules to choose from.
  • Community support is strong and steady. The selenium webdriver npm package receives regular updates. You can find guides, plugins, and solutions for nearly every use case.

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.