Maciej Bledkowski
Maciej Bledkowski

Reputation: 713

Puppeteer: ProtocolError: Waiting for selector failed: Protocol error: Argument should belong to the same JavaScript world as target object

I have created this function:

async function sendRequest(page: Page, url: string) {
  await Promise.all([
    page.waitForNavigation(),
    page.goto(url),
    page.waitForSelector("div#header"),
  ]);
}

When I use this function, NodeJS throws the following error message:

ProtocolError: Waiting for selector `div#header` failed: Protocol error (Runtime.callFunctionOn): Argument should belong to the same JavaScript world as target object
    at /home/mble/Code/repos/pluget/services/spigotScraper/node_modules/puppeteer-core/src/common/Connection.ts:400:16
    at new Promise (<anonymous>)
    at CDPSessionImpl.send (/home/mble/Code/repos/pluget/services/spigotScraper/node_modules/puppeteer-core/src/common/Connection.ts:396:12)
    at ExecutionContext._ExecutionContext_evaluate (/home/mble/Code/repos/pluget/services/spigotScraper/node_modules/puppeteer-core/src/common/ExecutionContext.ts:274:44)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at WaitTask.rerun (/home/mble/Code/repos/pluget/services/spigotScraper/node_modules/puppeteer-core/src/common/WaitTask.ts:123:26)
[ERROR] 18:53:05 ProtocolError: Waiting for selector `div#header` failed: Protocol error (Runtime.callFunctionOn): Argument should belong to the same JavaScript world as target object

How can I solve this issue?

Upvotes: 5

Views: 3093

Answers (2)

gieoon
gieoon

Reputation: 177

In my case I got this because I was calling element handles from the previous page after navigating to a new page via page.goto()

Make sure your elements exist!!!

Upvotes: 1

marko-36
marko-36

Reputation: 1496

Ahoj Macieji, I am using puppeteer-extra too, but I am not sure if that is the cause. The error, however, is the same as in your case:

Argument should belong to the same JavaScript world as target object

This happens, after the page.goto runs, page.waitForSelector is set and then navigation occurs (e.g. from an authentication page, in my case). I guess this results in a "different Javascript world", and the error is triggered.

A workaround for me was to first page.waitForSelector and then page.goto. Weird, but I hope it works for you too.

Upvotes: 2

Related Questions