How to Install and use Selenium WebDriver with NPM for JavaScript Automation

On This Page What is Selenium WebDriver?What is NPM?

April 15, 2026 · 10 min read · Tool Comparison

How to Install and use Selenium WebDriver with NPM for JavaScript Automation

InstallingSelenium WebDriver using NPMsimplifies JavaScript exam automation by managing dependencies efficiently. With only a few bid, you can set up WebDriver, mix it into your project, and start automating browser interaction.

Overview

Why Install and Use Selenium WebDriver with NPM?

  • Easy Installation– Quickly set up Selenium WebDriver with a single command.
  • Seamless Dependency Management– NPM handles package update and compatibility.
  • Fast Integration– Works with frameworks like Mocha, Jest, and Cucumber.
  • Cross Browser Testing– Automate test across Chrome, Firefox, and more.
  • Scalability– Easily manage and execute big test suites efficiently.

This guide walks you through the installation process to get Selenium WebDriver running with NPM seamlessly.

What is Selenium WebDriver?

is an open-source creature for automating browser interactions. You can write your scripts in language like JavaScript to control browser actions such as clicking, typing, and navigating. WebDriver works immediately with the browser, thus offering faster and more reliable automation than the previous Selenium tools.

Selenium is one of the top rival for JavaScript automation testing because it integrates very good with JavaScript framework. It is apply for task automation, and ensures that the web application under test works easily on different browser. Selenium besides supports crisscross and, hence, has go a versatile tool for any signifier of web mechanization needs.

Read More:

What is NPM?

NPM (Node Package Manager)is the default package manager forNode.js, used to instal, manage, and share JavaScript libraries and dependencies. It simplify the development summons by allowing developers to easy integrate external packages into their projects.

Key Features of NPM

Here are the key feature of:

  • Package Management: Install, update, and remove JavaScript libraries effortlessly.
  • Dependency Handling: Automates dependency management for smooth undertaking setup.
  • Version Control: Ensures compatibility by managing different package versions.
  • Global & amp; Local Installation: Supports installing packages for specific projects or system-wide use.
  • Scripts & amp; Automation: Allows running custom scripts for task like test, building, and deployment.

Read More:

Pre-requisites for Selenium WebDriver Installation

Before you get part with mechanisation using Selenium WebDriver and JavaScript, some setup is necessitate. These setup requirements ensure that everything runs smoothly, so you & # 8217; re good to go and ready to automatise browser interactions.

1. Node.js and NPM: Selenium WebDriver for JavaScript is make on Node.js and NPM (Node Package Manager). Node.js is the runtime environment in which you run JavaScript outside a browser.

NPM is a library coach, for example, and Selenium & # 8217; s subordinate library and libraries. So install both.

NPM isn & # 8217; t just for managing —it handgrip all undertaking dependency, ensuring seamless package installation, variant control, and efficient project management.

2. JavaScript Knowledge: Selenium WebDriver sends messages to and receives responses from web pages with the help of JavaScript.

It can be helpful if you know how to compose simple scripts, deal asynchronous code, and interact with web elements using JavaScript.

3. Web Browser: Selenium supports all major browsers, such as,,, and Edge. You must install and update the browser you would wish to automate.

4. WebDriver: You will need the WebDriver appropriate to the type of browser you are automating.

You will need the for Chrome and the GeckoDriver for Firefox. These drivers act as a interlocutor between Selenium and the browser so the WebDriver can check the browser.

5. Text Editor/IDE: Choose a text editor or an Integrated Development Environment (IDE) like Sublime Text, Visual Studio Code, or Atom to compose your JavaScript code.

Read More:

Steps to Install Selenium WebDriver with NPM

Installing Selenium WebDriver with NPM for JavaScript automation is a straightforward process. Follow the steps below to set up your environs and start using Selenium in your projects.

Step 1: Install Node.js and NPM

If you haven & # 8217; t already, download and install Node.js. The installation of Node.js mechanically includes NPM, which you & # 8217; ll use to manage your dependencies. You can verify that Node.js and NPM are installed aright by running the following commands in your terminal:

node -v npm -v

These bid should return the version numbers of Node.js and NPM installed on your system.

Step 2: Create a New Project Folder

Create a new directory for your Selenium automation project. Open your terminal or command prompt and run the following command to create a new pamphlet:

mkdir selenium-automation cd selenium-automation

This will create a new booklet named selenium-automation and navigate you into it.

Step 3: Initialize a New Node.js Project

In your labor folder, initialise a new Node.js project by running:

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

npm init -y

This will create a package.json file, which will store the details of your project and its addiction.

Step 4: Install Selenium WebDriver

Now that your projection is set up, it & # 8217; s time to instal Selenium WebDriver. Run the undermentioned command to install the Selenium package:

npm install selenium-webdriver -- save

This will download and install the Selenium WebDriver bundle into your project ’ s node_modules folder and add it as a dependency in your package.json file.

Step 5: Install Browser Driver

Depending on the browser you desire to automate, you & # 8217; ll need to install the respective WebDriver. For example, if you & # 8217; re using Chrome, you necessitate to download ChromeDriver.

After downloading the driver, ensure it ’ s accessible by adding the location to your system & # 8217; s PATH or specify its route now in your Selenium script.

Read More:

Step 6: Verify the Installation

To ensure that everything is set up correctly, create a simple JavaScript file, for example, test.js, in your task pamphlet. Add the following code to open a browser apply Selenium:

const {Builder} = require ('selenium-webdriver '); const driver = new Builder () .forBrowser ('chrome ') .build (); driver.get ('http: //www.google.com ') .then (() = & gt; driver.quit ());

Save the file and run it in the depot with:

node test.js

If everything is set up right, this book will open Chrome, navigate to Google ’ s homepage, and so close the browser.

Talk to an Expert

Setting up and Running the Selenium Script

Once you & # 8217; ve installed Selenium WebDriver and set up your project, you can start compose and pass Selenium automation scripts. Here ’ s how to set up and execute your first Selenium script.

Step 1: Create a New JavaScript File

In your project folder, make a new JavaScript file, such as automation.js. This file will contain the code that automatize the browser interaction.

Read More:

Step 2: Import the Required Selenium Modules

To interact with Selenium WebDriver, you foremost need to import the necessary module. At the top of your JavaScript file, add the undermentioned code:

const {Builder, By, Key, until} = require ('selenium-webdriver ');
  • Builder:Helps create a new WebDriver instance.
  • By:Allows you to place web elements on the page (e.g., by ID, gens, or class).
  • Key:Used for simulating keyboard inputs.
  • until:Helps you define weather that the script should look for (e.g., until an factor is visible).

Step 3: Set Up the WebDriver

Next, you need to make a new instance of the WebDriver to control the browser. In this example, use Chrome, but you can use early browsers by modifying the browser parameter. Here & # 8217; s how you set up the WebDriver for Chrome:

let driver = new Builder () .forBrowser ('chrome ') .build ();

Step 4: Navigate to a Website

After setting up the WebDriver, the next step is to navigate to a website.

driver.get ('http: //www.google.com ');

This command will open Google in the browser you specified (Chrome in this case).

Step 5: Interact with Web Elements

Selenium WebDriver allows you to interact with assorted elements on a web page. You can observe component by their ID, name, class, and more. Let ’ s say you require to search for a term on Google. You can locate the search box by its name (q) and type in a query:

driver.findElement (By.name (' q ')) .sendKeys ('Selenium WebDriver ');

This line finds the search box on Google and types the search term & # 8220;Selenium WebDriver& # 8221; into it.

Read More:

Step 6: Submit the Search

To submit the lookup query, you can simulate pressing the & # 8216;Enter& # 8216; key using Selenium & # 8217; s Key faculty. Here & # 8217; s how you do that:

driver.findElement (By.name (' q ')) .sendKeys (Key.RETURN);

Step 7: Wait for an Element to Appear (Optional)

Selenium allows you to wait for elements to go available before execute actions. For example, if you want to wait for the Google event to load, you can use until to wait for a specific condition. Here ’ s how to look until the search result appear:

driver.wait (until.elementLocated (By.id ('search ')), 10000);

This waits for the element with the ID search (the results container) to appear, with a timeout of 10 seconds.

Step 8: Close the Browser

Once the automation project are dispatch, you should close the browser to free up resources. You can do this by calling the quit () method on the driver:

driver.quit ();

Read More:

Step 9: Run the Script

Now that you ’ ve set up your script, you can run it. Open your terminal and navigate to your project leaflet. Run the script with the next command:

node automation.js

If everything is set up correctly, the handwriting will open Google in Chrome, type & # 8220;Selenium WebDriver& # 8221; in the search box, state the search, wait for the results, and then close the browser.

Why should you install and use Selenium WebDriver with NPM?

Using Selenium WebDriver with NPMstreamlines the automation process by simplifying installment, dependence direction, and integrating with JavaScript framework. Here ’ s why it ’ s beneficial:

  • Easygoing Installation: Setting upSelenium WebDriverwith NPM is quick and straightforward. With a single command, you can install WebDriver and its dependence without manual downloads or configurations.
  • Seamless Dependency Management: NPM automatically handles package dependencies, insure that all required faculty are installed and update without compatibility issues.
    This simplify maintenance and direction of test automation projects.
  • Fast Integration with Test Frameworks: Selenium WebDriver work seamlessly with popular JavaScript prove frameworks likeMocha, Jest, andCucumber,allowing developers to write and execute automated tests efficiently.
  • Cross Browser Testing: Using Selenium WebDriver with NPM enables automation across multiple browsers such asChrome, Firefox, Edge, and Safari,ensuring consistent web application execution across different environments.
  • Scalability and Efficiency: With NPM, you can easily grapple large test suites, integrate WebDriver into CI/CD pipelines, and execute parallel tryout, get automation testing more scalable and effective.

Why run Selenium WebDriver Tests on Real Devices & amp; Browsers?

While emulators and Virtual Machines offer convenience, test on real device ensures precise, real-world performance. Here ’ s why it matter:

  • Accurate User Experience: Real device capture network fluctuations, device quirks, and hardware variations that emulators miss.
  • : Different browsers render code unambiguously. Testing on real browsers ensures a undifferentiated experience across Chrome, Firefox, Safari, and more.
  • Device-Specific Testing: Real device support touch gestures, screen orientation changes, and hardware-specific interactions crucial for mobile testing.
  • Performance & amp; Speed Validation: Emulators often lag; real devices furnish true speed and reactivity insights.
  • Network Variability: Simulate real-world conditions like slow connections or unstable peregrine data to test app behavior.
  • Comprehensive Test Coverage: Access a wide range of devices and browsers to ensure broad compatibility.
  • Better Debugging: Identify device-specific bugs, rendering issues, and performance bottlenecks more effectively.
  • Quality Assurance & amp; User Confidence: Testing on real devices minimizes post-release issues, ensuring a refined, dependable application.

Your application can work appropriately across environments and have peak execution, offering optimum user experience when executing Selenium trial on genuine devices and browser.

With, you can test on 3,500+ real devices and browsers without setup hassles. Run Selenium WebDriver test on real mobile and desktop environments to ensure your covering delivers a seamless experience across different platform. It helps you integrate with pipeline, enable, debug faster and release with confidence.

Conclusion

Installing Selenium WebDriver with NPM is a straight summons that enable seamless JavaScript-based test mechanization. By setting up WebDriver, managing dependencies with NPM, and leveraging WebDriver & # 8217; s capability, you can efficiently automate browser interactions across different environs. Whether for local testing or running tests on cloud platforms like, integrating Selenium with NPM streamlines your automation workflow. With this setup, you can write, execute, and scale reliable test playscript, guarantee robust web coating quality.

BrowserStack Automate makes prove leisurely on many real devices and browsers without fix up a physical device lab. It enhances test coverage and improves debugging to deliver high-quality web coating that work good in most environments.

Tags
23,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free