Integrate with Puppeteer

Puppeteer is a Node.js library which provides a high-level API to control Chrome or Firefox over the DevTools Protocol or WebDriver BiDi. Puppeteer runs in the headless (no visible UI) by default but can be configured to run in a visible ("headful") browser.

Demo Project

Preparation

Config the OpenAI API key, or config model and provider

# replace with your own
export OPENAI_API_KEY="sk-abcdefghijklmnopqrstuvwxyz"

Step 1. install dependencies

npm
yarn
pnpm
bun
npm install @midscene/web puppeteer tsx --save-dev

Step 2. write scripts

Write and save the following code as ./demo.ts.

./demo.ts
import puppeteer from "puppeteer";
import { PuppeteerAgent } from "@midscene/web/puppeteer";

const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
Promise.resolve(
  (async () => {
    const browser = await puppeteer.launch({
      headless: false, // here we use headed mode to help debug
    });

    const page = await browser.newPage();
    await page.setViewport({
      width: 1280,
      height: 800,
      deviceScaleFactor: 1,
    });

    await page.goto("https://www.ebay.com");
    await sleep(5000);

    // 👀 init Midscene agent
    const agent = new PuppeteerAgent(page);

    // 👀 type keywords, perform a search
    await agent.aiAction('type "Headphones" in search box, hit Enter');
    await sleep(5000);

    // 👀 understand the page content, find the items
    const items = await agent.aiQuery(
      "{itemTitle: string, price: Number}[], find item in list and corresponding price"
    );
    console.log("headphones in stock", items);

    // 👀 assert by AI
    await agent.aiAssert("There is a category filter on the left");

    await browser.close();
  })()
);

Step 3. run

Using tsx to run, you will get the data of Headphones on eBay:

# run
npx tsx demo.ts

# it should print 
#  [
#   {
#     itemTitle: 'JBL Tour Pro 2 - True Wireless Noise Cancelling earbuds with Smart Charging Case',
#     price: 551.21
#   },
#   {
#     itemTitle: 'Soundcore Space One无线耳机40H ANC播放时间2XStronger语音还原',
#     price: 543.94
#   }
# ]

For the agent's more APIs, please refer to API.

Step 4: view the report

After the above command executes successfully, the console will output: Midscene - report file updated: /path/to/report/some_id.html. You can open this file in a browser to view the report.

More options in PuppeteerAgent constructor

To limit the popup to the current page

If you want to limit the popup to the current page (like clicking a link with target="_blank"), you can set the forceSameTabNavigation option to true:

const mid = new PuppeteerAgent(page, {
  forceSameTabNavigation: true,
});

More

You may also be interested in Prompting Tips