const doesContainAllParts: AlertsProximityAnalyzerComparator< Frame | Page > = async (element) => { try { const test = element.locator('body'); const result = await test.count(); // Page closes unexpectedly here return result > 0; } catch (error) { console.error('Error in doesContainAllParts:', error); throw error; } };
const result = await test.count()
. Each time this line executes, the page closes, leading to the failure of the operation. await
operation on the locator instance.element
variable there? I dont see it in the debugger variables toolbar, is it just regular page
object?private requestHandler: PlaywrightRequestHandler = async ({ page, request, log, }) => { log.info(`Request to: ${request.url} ...`) await page.waitForLoadState('domcontentloaded') const title = await page.title() // works fine const content = await page.content() // works fine // every other logic I paste here works fine, but i cannot paste it here because of my buissness logic and other dynamic data I provide here. await playwrightUtils.infiniteScroll(page, { scrollDownAndUp: true, waitForSecs: 2, timeoutSecs: 5, }) this.resolvePromise(request.url, page) } ==== private resolvePromise(url: string, result: Page): void { if (this.urlToPromiseResolver[url]) { this.urlToPromiseResolver[url].resolvePromise(result) delete this.urlToPromiseResolver[url] } } ==== public resolve = async (urls: string[]): Promise<Page[]> => { const urlsWithUniqueKeys = urls.map((url) => ({ url, uniqueKey: `${url}_${Math.random()}`, })) await this.crawler.addRequests(urlsWithUniqueKeys) const promises = urls.map((url) => { return new Promise<Page | null>((resolvePromise, rejectPromise) => { this.urlToPromiseResolver[url] = { resolvePromise, rejectPromise } }) }) const result = Promise.all(promises) .then(filterResults).cathc(...) return result } ==== Thats how I use it: const [page] = await playwrightCrawleePageResolver.resolve([url]) const title = await page.title() //error
<body>
element is <html>
element - That is why I asked for the value of element
parameter there. I already linked you official documentation on how to work with frames in Playwright, and unfortunately we don't know anything about the website, that you are scraping. I suggest you to check the link I have provided and maybe some further examples, to get the idea on how to scrape frames/iframes.requestHandler
callback) so I manage this problem by providing call back to my functionplaywrightCrawleePageResolver.resolve([url], callback)
and this callback is executed in requestHandler
PlaywrightCrawler handles crawling (the page is open only within the scope of requestHandler callback)Yes it does, so you should keep all the logic withing the requestHandler, of course you may call your own functions and methods, just keep in mind, when calling async functions use the
await
keyword, otherwise the is no waiting for the results of your function and the page might get close before your function being evaluated.