October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk5 min

How to Use Selenium with JavaScript

Install Selenium’s Node.js binding, automate a browser, verify a page result, and close sessions reliably—with guidance on drivers, remote runs, and common failures.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To use Selenium with JavaScript, install the selenium-webdriver npm package, create a WebDriver session with Builder, await browser commands, and always close the session with driver.quit(). The current Selenium JavaScript API lists Node.js 22 or newer as its requirement; check the official API documentation for current compatibility details.

Install Selenium for Node.js

In a new or existing Node.js project, install Selenium’s JavaScript binding:

npm install selenium-webdriver

The package is named selenium-webdriver. At the time reflected in the current API documentation, supported Node.js lines are 22, 24, and 26; their listed support end dates are April 30, 2027, April 30, 2028, and April 30, 2029, respectively. These are version-sensitive details, so confirm them in the live JavaScript API documentation before choosing a runtime for a longer-lived project.

Run a first Selenium script

Save this CommonJS example as selenium.js in the project. It opens Chrome, visits Selenium’s sample form, enters text, submits it, checks the response, and quits even if an operation or assertion fails.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const { By, Builder, Browser } = require('selenium-webdriver');
const assert = require('node:assert/strict');

(async function run() {
  const driver = await new Builder().forBrowser(Browser.CHROME).build();
  try {
    await driver.get('https://www.selenium.dev/selenium/web/web-form.html');

    const input = await driver.findElement(By.name('my-text'));
    const submit = await driver.findElement(By.css('button'));
    await input.sendKeys('Selenium');
    await submit.click();

    const message = await driver.findElement(By.id('message'));
    assert.equal(await message.getText(), 'Received!');
  } finally {
    await driver.quit();
  }
})();

Run it with:

node selenium.js

WebDriver calls are asynchronous in the JavaScript binding. Await navigation, element lookup, typing, clicking, and reading text; otherwise code may move on before the browser operation finishes. The finally block ensures that the browser session is closed after a failure as well as after a successful assertion.

Choose a browser and locate elements

Select the browser

The Builder configuration selects the browser. For Chrome, the example uses .forBrowser(Browser.CHROME). Firefox can be selected with .forBrowser(Browser.FIREFOX); browser-specific configuration belongs on the Builder setup. The target browser must be available in the environment where the session runs.

Use stable locators

Selenium’s By helpers locate elements by attributes or selectors. The sample uses By.name('my-text'), By.css('button'), and By.id('message'). Prefer stable page semantics or test-owned attributes where the application provides them. Broad selectors such as a generic button selector can become ambiguous when a page changes; narrow the selector to the intended control if that happens.

Wait for the page condition you need

Do not assume that an arbitrary sleep is the right synchronization strategy. Choose a wait based on the page’s behavior—for example, wait until a result element is present or visible when the application updates asynchronously. A fixed delay can be too short on a slow run and waste time on a fast one. Selenium’s JavaScript setup material shows timeout configuration and test-runner setup/teardown patterns in its organizing and executing Selenium code guide.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How Selenium Manager handles browser drivers

WebDriver is a language-neutral API and protocol for controlling browsers; a browser-specific driver mediates communication between Selenium and the browser. Selenium Manager is the Selenium project’s official driver manager and ships with Selenium releases. When a driver has not already been supplied, Selenium bindings use Selenium Manager as a fallback. It can identify the installed browser version, resolve and download a compatible driver, and cache it locally. Automated management of browser releases as well as drivers was added in Selenium 4.11.0. See the Selenium Manager documentation for details.

For an ordinary local setup, start with the default Builder and let Selenium Manager resolve the driver. You can still manage drivers yourself when an environment requires explicit control. For remote execution, configure the server URL instead of treating the remote browser as a local installation.

Run Selenium on a remote Grid or server

A local run starts a session against a browser provisioned on the machine running the Node.js script. A remote run sends commands to a Selenium Grid or standalone server, which owns the browser session and its browser/driver provisioning. The official JavaScript API documents both Builder server configuration and the SELENIUM_REMOTE_URL setting.

const { Builder, Browser } = require('selenium-webdriver');

const driver = await new Builder()
  .forBrowser(Browser.CHROME)
  .usingServer('http://localhost:4444')
  .build();

Use the actual reachable URL of your Grid or server in place of the local example. The Builder route and server URL determine where the browser runs; they do not install or provision a remote browser for you. Consult the API reference for the current remote configuration options.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common setup failures

  • Node version is unsupported: Check node --version and compare it with the Node.js requirement in the live Selenium JavaScript API documentation. Upgrade to a currently supported Node.js line if needed.
  • Session creation cannot find or start the browser: Confirm the selected browser is installed and available to the process. With the default Builder, Selenium Manager needs to resolve a compatible driver; check that the environment can reach its metadata and download endpoints.
  • Driver and browser versions do not match: Let Selenium Manager handle the missing driver first. If you intentionally pin or provide a driver, verify that it is compatible with the browser version installed in that environment.
  • The script exits before an action completes: Await each WebDriver operation, including element lookup and result reads. Keep session cleanup in finally or the test framework’s teardown hook.
  • An element lookup fails or returns the wrong element: Check that the page has reached the expected state, then verify the locator against the current DOM. Prefer a specific stable attribute or selector over a broad match.
  • A remote session cannot connect: Verify that the Grid or standalone server is running and reachable from the Node.js process, and that the configured URL points to that server rather than to a local browser.

Or skip the browser setup

If your goal is a screenshot rather than browser interaction and assertions, ScreenshotNeo can return an image or PDF from one API request. Its API can accept consent banners and remove more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the page verdict and billing status in headers. An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents.

For example, the following cURL request saves a WebP screenshot; replace the URL with the page you need and provide your API key. See the ScreenshotNeo API documentation for request options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Sign up for 1,000 free screenshots a month, with no card required.

Sources and further reading

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.