Interacting with a Page
Overview
This recipe demonstrates how to use Airtop to automate human interactions with a browser and extract stock performance data. It also makes use of Airtop’s page query API to fetch and process data from the web page.
The instructions below will guide you through creating a script that:
- Connects to Airtop
- Initializes a browser session and window
- Navigates to a stock performance page using click and type interactions
- Extracts data using the page query API
- Displays the data in a formatted manner
Demo
A live demo of this recipe is available here. You can sign up to create an API key for free and try it out yourself!
Prerequisites
To get started, ensure you have:
- Node.js installed on your system.
- PNPM package manager installed. See here for installation steps.
- A Node Version Manager (NVM preferably).
- An Airtop API key. You can get one for free.
Getting Started
-
Clone the repository
Start by cloning the source code from GitHub:
-
Setup
Follow the README instructions in the root of the cloned repository to install dependencies and build packages. Then follow the instructions in
examples/simple-interactions/README.md
.
Script Walkthrough
The script in simple-interactions.cli.ts
performs the following tasks:
-
Initialize the Logger and Prompt for API Key
The script starts by initializing a logger and prompting the user for their Airtop API key.
-
Create an InteractionsService Instance
An instance of
InteractionsService
is created using the provided API key, logger and a default ticker symbol. This service handles interactions with the browser. -
Initialize Session and Browser
The script initializes a session and browser window using the
initializeSessionAndBrowser
method fromInteractionsService
.Under the Hood:
-
Search for Stock Performance
The script uses the
searchForStockPerformance
method to search for stock performance data.Under the Hood:
-
Click on Stock Performance Chart
The script simulates a click on the stock performance chart using the
clickOnStockPerformanceChart
method.Under the Hood:
-
Extract Stock Performance Data
The script extracts stock performance data using the
extractStockPerformanceData
method and formats it for display.Under the Hood:
-
Clean Up
Finally, the script ensures that the session is terminated to clean up resources.
Under the Hood:
Running the Script
To run the script, execute the following command in your terminal:
This command uses the cli
script defined in the package.json
file, which runs the simple-interactions.cli.ts
file using tsx
.
Summary
This recipe demonstrates how Airtop can be utilized to automate tasks that require browser interactions which would otherwise require manual intervention. Compounded with other Airtop APIs like the page query API, it can be used to build powerful automation workflows.