Skip to content

Commit a165c36

Browse files
kathaylToriLindsay
andauthored
Update puppeteer.mdx (cloudflare#25075)
* Update puppeteer.mdx set custom user agent on puppeteer * Update playwright.mdx Setting custom user agent in Playwright * Update playwright.mdx fix extra line * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/puppeteer.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/playwright.mdx Co-authored-by: ToriLindsay <[email protected]> * Update src/content/docs/browser-rendering/platform/puppeteer.mdx Co-authored-by: ToriLindsay <[email protected]> --------- Co-authored-by: ToriLindsay <[email protected]>
1 parent e427e1b commit a165c36

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/content/docs/browser-rendering/platform/playwright.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,22 @@ const browser = await playwright.launch(env.MYBROWSER, { keep_alive: 600000 });
201201

202202
Using the above, the browser will stay open for up to 10 minutes, even if inactive.
203203

204+
### Set a custom user agent
205+
206+
To specify a custom user agent in Playwright, set it in the options when creating a new browser context with `browser.newContext()`. All pages subsequently created from this context will use the new user agent. This is useful if the target website serves different content based on the user agent.
207+
208+
```js
209+
const context = await browser.newContext({
210+
userAgent:
211+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
212+
});
213+
```
214+
215+
:::note
216+
The `userAgent` parameter does not bypass bot protection. Requests from Browser Rendering will always be identified as a bot.
217+
:::
218+
219+
204220
## Session management
205221

206222
In order to facilitate browser session management, we have extended the Playwright API with new methods:

src/content/docs/browser-rendering/platform/puppeteer.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ const browser = await puppeteer.launch(env.MYBROWSER, { keep_alive: 600000 });
5757

5858
Using the above, the browser will stay open for up to 10 minutes, even if inactive.
5959

60+
### Set a custom user agent
61+
62+
To specify a custom user agent in Puppeteer, use the `page.setUserAgent()` method. This is useful if the target website serves different content based on the user agent.
63+
64+
```js
65+
await page.setUserAgent(
66+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
67+
);
68+
```
69+
70+
:::note
71+
The `userAgent` parameter does not bypass bot protection. Requests from Browser Rendering will always be identified as a bot.
72+
:::
73+
6074
## Session management
6175

6276
In order to facilitate browser session management, we've added new methods to `puppeteer`:

0 commit comments

Comments
 (0)