Selenium

Setup a browser session with Selenium.

1

Install Selenium and Hyperbrowser

npm install selenium-webdriver @hyperbrowser/sdk

or

yarn add selenium-webdriver @hyperbrowser/sdk
2

Setup your Environment

To use Hyperbrowser with your code, you will need an API Key. You can get one easily from the dashboard. Once you have your API Key, add it to your .env file as HYPERBROWSER_API_KEY .

3

Setup Browser Session

Next, you can easily startup a browser session using selenium and your Hyperbrowser API Key.

import dotenv from 'dotenv';
import https from 'https';
import { Builder, WebDriver } from 'selenium-webdriver';
import { Options } from 'selenium-webdriver/chrome';
import { Hyperbrowser } from '@hyperbrowser/sdk';

// Load environment variables from .env file
dotenv.config();

const client = new Hyperbrowser({ apiKey: process.env.HYPERBROWSER_API_KEY as string });

async function main() {
    const session = await client.sessions.create();
    
    const customHttpsAgent = new https.Agent({});
    (customHttpsAgent as any).addRequest = (req: any, options: any) => {
        req.setHeader('x-hyperbrowser-token', session.token);
        (https.Agent.prototype as any).addRequest.call(customHttpsAgent, req, options);
    };

    const driver: WebDriver = await new Builder()
        .forBrowser('chrome')
        .usingHttpAgent(customHttpsAgent)
        .usingServer('https://connect.hyperbrowser.ai/webdriver')
        .setChromeOptions(new Options())
        .build();

    try {
        // Navigate to a URL
        await driver.get("https://www.google.com");
        console.log("Navigated to Google");
        
        // Search
        const searchBox = await driver.findElement({ name: "q" });
        await searchBox.sendKeys("Selenium WebDriver");
        await searchBox.submit();
        console.log("Performed search");
        
        // Screenshot
        await driver.takeScreenshot().then(data => {
            require('fs').writeFileSync('search_results.png', data, 'base64');
        });
        console.log("Screenshot saved");
    } finally {
        await driver.quit();
    }
}

if (require.main === module) {
    main().catch(console.error);
}
4

View Session in Dashboard

You can view all your sessions in the dashboard and see their recordings or other key metrics like logs.

Last updated

Logo

© 2025 S2 Labs, Inc. All rights reserved.