Skip to content

Commit c095b29

Browse files
committed
docs: move XPath selector FAQ to Puppeteer page
- Add Element selection section to Puppeteer documentation - Explain XPath limitation and provide page.evaluate() workaround - Remove duplicate XPath FAQ entry from FAQ page - Improve information architecture by placing content in relevant context
1 parent 81edf34 commit c095b29

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

src/content/docs/browser-rendering/faq.mdx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,6 @@ There is no fixed limit on the number of requests per browser session. A single
5454

5555
This error typically occurs because your Puppeteer launch is not receiving the Browser binding. To resolve: Pass your Browser binding into `puppeteer.launch`.
5656

57-
### Why can't I use an XPath selector when using Browser Rendering with Puppeteer?
58-
59-
Currently it is not possible to use Xpath to select elements since this poses a security risk to Workers.
60-
61-
As an alternative, try to use a css selector or `page.evaluate`. For example:
62-
63-
```ts
64-
const innerHtml = await page.evaluate(() => {
65-
return (
66-
// @ts-ignore this runs on browser context
67-
new XPathEvaluator()
68-
.createExpression("/html/body/div/h1")
69-
// @ts-ignore this runs on browser context
70-
.evaluate(document, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue
71-
.innerHTML
72-
);
73-
});
74-
```
75-
76-
:::note
77-
78-
Keep in mind that `page.evaluate` can only return primitive types like strings, numbers, etc. Returning an `HTMLElement` will not work.
79-
80-
:::
81-
8257
### Why is my screenshot blurry?
8358

8459
It may be because you increased the height and width of the viewport. To fix this, increase the value of the `deviceScaleFactor` (default is 1).

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,32 @@ await page.setUserAgent(
6868
```
6969

7070
:::note
71-
The `userAgent` parameter does not bypass bot protection. Requests from Browser Rendering will always be identified as a bot.
71+
The `userAgent` parameter does not bypass bot protection. Requests from Browser Rendering will always be identified as a bot.
72+
:::
73+
74+
## Element selection
75+
76+
Puppeteer provides multiple methods for selecting elements on a page. While CSS selectors work as expected, XPath selectors are not supported due to security constraints in the Workers runtime.
77+
78+
Instead of using Xpath selectors, you can use CSS selectors or `page.evaluate()` to run XPath queries in the browser context:
79+
80+
```ts
81+
const innerHtml = await page.evaluate(() => {
82+
return (
83+
// @ts-ignore this runs on browser context
84+
new XPathEvaluator()
85+
.createExpression("/html/body/div/h1")
86+
// @ts-ignore this runs on browser context
87+
.evaluate(document, XPathResult.FIRST_ORDERED_NODE_TYPE).singleNodeValue
88+
.innerHTML
89+
);
90+
});
91+
```
92+
93+
:::note
94+
95+
`page.evaluate()` can only return primitive types like strings, numbers, and booleans. Returning complex objects like `HTMLElement` will not work.
96+
7297
:::
7398

7499
## Session management

0 commit comments

Comments
 (0)