Movianlost
Movianlost

Reputation: 366

What does page._client do in Puppeteer?

I read the whole Puppeteer documentation, but unfortunately, they don't have any documentation for the _client property of page.

So my question is, what is page._client ?

And what is the difference between using

await page._client.send('');

And

client = await page.target().createCDPSession()
await client.send('');

Upvotes: 6

Views: 8100

Answers (3)

user1463368
user1463368

Reputation: 21

page._client is used internally by puppeteer classes. As people have pointed out above, it is best, if not always to avoid using page._client as it is a private API.

Create your own CDP Sessions page.target().createCDPSession() to access the chrome devtools protocol directly. Things might work when you use page._client, but when you start to try to implement some of the low level features of the devtools protocol by yourself, there are going to be some collisions and bugs that arises that aren't documented anywhere and you will be left scratching your head wondering if chrome devtools is so broken.

Example of this errors is like when you could try to use the Fetch domain directly to do proxy authentication instead of using page.authenticate(...). Things are going to break, and you are going to try to search the errors which are nowhere to be found, look at puppeteer source code and see you aren't doing anything different, but guess what, since you used page._client.send(...) instead of creating your own CDPSession, you're going to pay the price of spending a whole day debugging your code.

@see: https://github.com/puppeteer/puppeteer/blob/v10.4.0/docs/api.md#class-cdpsession

Upvotes: 1

Hamada
Hamada

Reputation: 1898

There is no documentation for page._client.

Avoid page._client as it is a private API.

You can get a client object with await page.target ().

Upvotes: 1

Christian Fritz
Christian Fritz

Reputation: 21374

By JS convention, fields and methods that are prefixed with an underscore like _client are "private", and are not to be relied upon. This is almost certainly also the reason why it is not documented. You are using it at your own risk. In a newer version of puppeteer this field may be gone or do something entirely different.

Newer flavors of JavaScript have proper private fields and methods (prefixed with # in the class definition), so most likely puppeteer will convert these fields to proper private fields soon.

Upvotes: 7

Related Questions