What does page._client do in Puppeteer?
Asked Answered
G

3

6

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('');
Golter answered 5/6, 2021 at 22:5 Comment(2)
As you can see in the source code it's a CDPSession so apparently it's a private value for the current session.Ethelind
@wOxxOm is it better to use _client which return an already created CDPSession or create another CDPSession? also what does page.target() return ? Puppeteer has no documentation on what a target isGolter
F
6

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.

Fanchan answered 5/6, 2021 at 22:9 Comment(4)
But what does _client property represent in this context? I suspect that it may be the default CDP session associated with the page, but I'm not sure.Golter
My point is that it doesn't really matter what it represents. Even if you do find out what it means, it may mean something else very soon or just go away, so you can't really use it. Is there something you think you need it for?Fanchan
Well Puppeteer is just a wrapper on the CDP api, and since it doesn't have a complete coverage, sometimes you have to rely on private properties to do the job. I want to access the current CDP session without creating a new one, it seems like _client returns the current CDP session. Also, in my second code, I also don't understand what the word target() means in that context that's why I didn't want to use any of them before I understand what each one is doing.Golter
The _client field has now been made inaccessible. github.com/puppeteer/puppeteer/pull/8506 You can use _client() instead.Handicraft
H
1

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 ().

Heptavalent answered 5/6, 2021 at 23:52 Comment(0)
S
1

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

Serge answered 10/2, 2022 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.