> ## Documentation Index
> Fetch the complete documentation index at: https://hyperbrowser.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Connect with Selenium

> Control Hyperbrowser sessions using Selenium

Selenium is a powerful browser automation framework that works seamlessly with Hyperbrowser sessions. This guide shows you how to connect Selenium to your cloud browser sessions.

## Installation

First, install Selenium:

```bash npm theme={null}
npm install @hyperbrowser/sdk selenium-webdriver dotenv
```

```bash yarn theme={null}
yarn add @hyperbrowser/sdk selenium-webdriver dotenv
```

```bash pip theme={null}
pip install hyperbrowser selenium python-dotenv
```

```bash uv theme={null}
uv add hyperbrowser selenium python-dotenv
```

## Basic Connection

Connect Selenium to a Hyperbrowser session:

```typescript Node.js theme={null}
import dotenv from "dotenv";
import https from "https";
import { Builder, WebDriver } from "selenium-webdriver";
import fs from "fs";
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 });

async function main() {
  const session = await client.sessions.create();

  if (!session.webdriverEndpoint) {
    await client.sessions.stop(session.id);
    throw new Error("No webdriver endpoint found");
  }

  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(session.webdriverEndpoint)
    .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) => {
      fs.writeFileSync("search_results.png", data, "base64");
    });
    console.log("Screenshot saved");
  } finally {
    await driver.quit();
    await client.sessions.stop(session.id);
  }
}

main().catch(console.error);
```

```python Python theme={null}
import os
from dotenv import load_dotenv

from selenium import webdriver
from selenium.webdriver.remote.client_config import ClientConfig
from selenium.webdriver.remote.remote_connection import RemoteConnection
from selenium.webdriver.chrome.options import Options
from hyperbrowser import Hyperbrowser

# Load environment variables from .env file
load_dotenv()

client = Hyperbrowser(api_key=os.getenv("HYPERBROWSER_API_KEY"))


class CustomRC(RemoteConnection):
    _signing_key = None

    def __init__(self, server: str, token: str):
        super().__init__(
            client_config=ClientConfig(
                remote_server_addr=server,
                extra_headers={"x-hyperbrowser-token": token},
            )
        )


def main():
    session = client.sessions.create()
    driver = None

    try:
        custom_conn = CustomRC(session.webdriver_endpoint, session.token)
        driver = webdriver.Remote(custom_conn, options=Options())

        # Navigate to a URL
        driver.get("https://www.google.com")
        print("Navigated to Google")

        # Search
        search_box = driver.find_element("name", "q")
        search_box.send_keys("Selenium WebDriver")
        search_box.submit()
        print("Performed search")

        # Screenshot
        driver.save_screenshot("search_results.png")
        print("Screenshot saved")
    except Exception as e:
        print(f"Error: {e}")
    finally:
        if driver is not None:
            driver.quit()
        client.sessions.stop(session.id)


if __name__ == "__main__":
    main()
```

> [!WARNING]
>
> Please contact us at [info@hyperbrowser.ai](mailto:info@hyperbrowser.ai) to get access to using Selenium with Hyperbrowser.

## Next Steps

> [card] [Puppeteer Integration](https://hyperbrowser.ai/docs/sessions/puppeteer.md)
>
> Connect with Puppeteer

> [card] [Playwright Integration](https://hyperbrowser.ai/docs/sessions/lifecycle.md)
>
> Connect with Playwright

> [card] [Stealth Mode](https://hyperbrowser.ai/docs/sessions/stealth.md)
>
> Configure anti-detection

> [card] [Profiles](https://hyperbrowser.ai/docs/sessions/profiles.md)
>
> Persist browser state


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.