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

<CodeGroup>
  ```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
  ```
</CodeGroup>

## Basic Connection

Connect Selenium to a Hyperbrowser session:

<CodeGroup>
  ```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.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__(server)
          self._token = token

      def get_remote_connection_headers(self, parsed_url, keep_alive=False):
          headers = super().get_remote_connection_headers(parsed_url, keep_alive)
          headers.update({"x-hyperbrowser-token": self._token})
          return headers


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

      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:
          client.sessions.stop(session.id)


  if __name__ == "__main__":
      main()
  ```
</CodeGroup>

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

## Next Steps

<CardGroup cols={2}>
  <Card title="Puppeteer Integration" icon="node" href="/sessions/puppeteer">
    Connect with Puppeteer
  </Card>

  <Card title="Playwright Integration" icon="rotate" href="/sessions/lifecycle">
    Connect with Playwright
  </Card>

  <Card title="Stealth Mode" icon="user-secret" href="/sessions/stealth">
    Configure anti-detection
  </Card>

  <Card title="Profiles" icon="user" href="/sessions/profiles">
    Persist browser state
  </Card>
</CardGroup>
