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.

1import { Builder, until } from 'selenium-webdriver';
2import * as https from 'https';
3import chrome from 'selenium-webdriver/chrome';
4import { AirtopClient } from "@airtop/sdk";
5
6const client = new AirtopClient({ apiKey: "YOUR_API_KEY" });
7const session = await client.sessions.create();
8
9// Generate a custom HTTP agent to handle the authorization header
10const customHttpAgent = new https.Agent({});
11(customHttpAgent as any).addRequest = (req: any, options: any) => {
12 req.setHeader('Authorization', 'Bearer YOUR_API_KEY');
13 (https.Agent.prototype as any).addRequest.call(customHttpAgent, req, options);
14};
15
16// Create a new Chrome driver instance
17const driver = await new Builder()
18 .forBrowser('chrome')
19 .usingHttpAgent(customHttpAgent)
20 .usingServer(session.data.chromedriverUrl)
21 .build();
22
23// Open a new tab and navigate to the target URL
24await driver.switchTo().newWindow('tab');
25await driver.get("https://www.airtop.ai");
26await driver.wait(until.titleContains('Airtop'), 10000);
27
28// Get the page content
29const content = await driver.getPageSource();
30console.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.

Built with