Integrating with Selenium

How to use Selenium with Airtop

Selenium 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.

npm i selenium-webdriver

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.

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);

If you’re not already familiar with Selenium, you might want to check out their documentation to learn more about the library and its capabilities.