> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.airtop.ai/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.airtop.ai/_mcp/server.

# Integrating with Selenium

[Selenium](https://www.selenium.dev/) is a powerful automation framework for web browsers. Airtop provides a Selenium connector that allows you to use Selenium to automate your browser.

## Installation

You will need to install the `selenium-webdriver` package to use Selenium with Airtop.

**`NodeJS (npm)`**

```bash NodeJS (npm)
npm i selenium-webdriver
```

**`NodeJS (yarn)`**

```bash NodeJS (yarn)
yarn add selenium-webdriver
```

**`NodeJS (pnpm)`**

```bash NodeJS (pnpm)
pnpm add selenium-webdriver
```

**`Python`**

```python Python
pip install selenium
```

## Usage

Once you have created a session with Airtop, you can use the Selenium library to control the browser by connecting Selenium to the ChromeDriver endpoint provided by Airtop.

**`NodeJS`**

```typescript NodeJS
import { Builder, until } from 'selenium-webdriver';
import * as https from 'https';
import chrome from 'selenium-webdriver/chrome';
import { AirtopClient } from "@airtop/sdk";

const client = new AirtopClient({ apiKey: "YOUR_API_KEY" });
const session = await client.sessions.create();

// Generate a custom HTTP agent to handle the authorization header
const customHttpAgent = new https.Agent({});
(customHttpAgent as any).addRequest = (req: any, options: any) => {
  req.setHeader('Authorization', 'Bearer YOUR_API_KEY');
  (https.Agent.prototype as any).addRequest.call(customHttpAgent, req, options);
};

// Create a new Chrome driver instance
const driver = await new Builder()
  .forBrowser('chrome')
  .usingHttpAgent(customHttpAgent)
  .usingServer(session.data.chromedriverUrl)
  .build();

// Open a new tab and navigate to the target URL
await driver.switchTo().newWindow('tab');
await driver.get("https://www.airtop.ai");
await driver.wait(until.titleContains('Airtop'), 10000);

// Get the page content
const content = await driver.getPageSource();
console.log(content);
```

**`Python`**

```python Python
from airtop import Airtop
from selenium import webdriver
from selenium.webdriver.chrome.remote_connection import ChromeRemoteConnection

def create_airtop_selenium_connection(airtop_api_key, airtop_session_data, *args, **kwargs):
    class AirtopRemoteConnection(ChromeRemoteConnection):
        @classmethod
        def get_remote_connection_headers(cls, *args, **kwargs):
            headers = super().get_remote_connection_headers(*args, **kwargs)
            headers['Authorization'] = f'Bearer {airtop_api_key}'
            return headers

    return AirtopRemoteConnection(remote_server_addr=airtop_session_data.chromedriver_url, *args, **kwargs)

# Initialize Airtop client
client = Airtop(api_key=api_key)
session = client.sessions.create()

# Connect to the Airtop cloud browser with Selenium and navigate to a page.
try:
  browser = webdriver.Remote(
    command_executor=create_airtop_selenium_connection(api_key, session.data),
    options=webdriver.ChromeOptions(),
  )
  browser.get("https://en.wikipedia.org/wiki/Rocket")

  # Get the window info and scrape the page content
  window_info = client.windows.get_window_info_for_selenium_driver(
    session.data,
    browser,
  )
  print(f"Live view url: {window_info.data.live_view_url}")
  scrape = client.windows.scrape_content(session_id=session.data.id, window_id=window_info.data.window_id, time_threshold_seconds=60)
  print(scrape.data.model_response.scraped_content)

  browser.quit()
finally:
  # Terminate the Airtop session.
  client.sessions.terminate(session.data.id)
```

If you're not already familiar with Selenium, you might want to check out their [documentation](https://www.selenium.dev/documentation/) to learn more about the library and its capabilities.