withBrowser(api, async ({ browser }) => {
const [page] = await browser.pages();
if (!page) return { error: "Can not find the first page in this window." };
await page.goto("https://wikipedia.org");
const englishButton = await page.waitForSelector("#js-link-box-en > strong");
await englishButton?.click();
const searchBox = await page.waitForSelector("#searchInput");
await searchBox?.type(searchWord);
await page.keyboard.press("Enter");
await Promises.delay(1000);
const headers = await page.evaluate(() => {
return [
...document.querySelectorAll("#bodyContent :is(h1, h2, h3, h4, h5, h6)")
].map((elm) => {
return {
tag: elm.tagName,
content: elm.innerText
};
});
});
const content = await page.$("#content");
const screenshot = await content?.screenshot({
encoding: "base64"
});
const screenshotUrl = `data:image/png;base64,${screenshot}`;
await Promises.delay(1000);
mutable automationResult = {
headers,
screenshotUrl
};
})