#browser pdf - playwright script used?

5 messages · Page 1 of 1 (latest)

runic parcel
#

https://docs.browserless.io/ai-integrations/open-claw this page says if "openclaw browser pdf" is issued, playwright is actually required. Would it be possible to retrieve the playwright script generated when "openclaw browser pdf" is invoked?

reason for the ask - I have a session open with "openclaw browser navigate", but I want to subsequently use a custom playwright script to connect to the same session to do some further automation with the website that openclaw has opened. I cannot figure out a way to do it. But assuming the "openclaw browser pdf" uses playwright to access the same session that was opened earlier with "openclaw browser navigate", I am hopeful by examining the generated playwright script, it will give me insight on how to connect to a persistent session. Please help @ebon arrow

ebon arrowBOT
#

I checked current source. Short answer: no, openclaw browser pdf does not generate a Playwright script you can retrieve.

What it does right now is basically:

const browser = await chromium.connectOverCDP(endpoint, { timeout, headers });
const page = await getPageForTargetId({ cdpUrl, targetId });
const pdf = await page.pdf({ printBackground: true });

So if you want your own Playwright script to attach to the same browser session, the path is:

  1. get the profile CDP endpoint
    openclaw browser --browser-profile <profile> status --json
  2. get the tab target id
    openclaw browser --browser-profile <profile> tabs --json
  3. attach yourself with Playwright
#

Example:

import { chromium } from "playwright";

const cdpUrl = process.env.CDP_URL;      // from status --json
const targetId = process.env.TARGET_ID;  // from tabs --json

const browser = await chromium.connectOverCDP(cdpUrl);
const pages = browser.contexts().flatMap(c => c.pages());

async function getTargetId(page) {
  const session = await page.context().newCDPSession(page);
  try {
    const info = await session.send("Target.getTargetInfo");
    return info?.targetInfo?.targetId ?? null;
#
  } finally {
    await session.detach().catch(() => {});
  }
}

const pairs = await Promise.all(pages.map(async p => [p, await getTargetId(p)]));
const page = pairs.find(([, id]) => id === targetId)?.[0] ?? pages[0];

await page.click("text=Whatever");

Important caveat: if you are using the user / existing-session profile, this will not work the same way. Current OpenClaw uses Chrome MCP there, not raw CDP, and pdf is explicitly unsupported for existing-session profiles. If you want to share a live session with your own Playwright code, use a managed openclaw profile or a raw cdpUrl profile.

Relevant docs:

#

If you want, I can turn this into a tiny ready-to-run helper script that reads the status --json and tabs --json output automatically.