For example the document.querySelectorAll
works with CSS selectors, but how can I use XPath expressions instead?
Asked
Active
Viewed 7,059 times
1
-
2there is an issue open, is it related to your requirement? https://github.com/GoogleChrome/puppeteer/issues/537 – Anil Nov 21 '17 at 12:18
-
yes,exactly!thanks! – microwth Nov 21 '17 at 15:36
2 Answers
4
They just released this feature :)
page.xpath(expression)
expression
<string>
Expression to evaluatereturns:
<Promise<?ElementHandle>>
Promise which resolves to ElementHandle pointing to the page element.
check it out here

Community
- 1
- 1

Horacio Alexandre Fernandes
- 865
- 1
- 9
- 12
-
1This feature is a moving target! It just got [renamed](https://github.com/GoogleChrome/puppeteer/commit/f183664d0f187649671d8d0086cbb016e0b24651) from `xpath` to `$x`. – jpaugh Jan 05 '18 at 00:20
-
`UnhandledPromiseRejectionWarning: TypeError: page.$x is not a function` – toing_toing Jun 21 '18 at 13:19
-
1@toing_toing that happens. But you can solve it as described in https://stackoverflow.com/a/48474929/5131858 const featureArticle = (await page.$x("//a[text()='abc.com']"))[0]; const text = await page.evaluate(el => {return el.textContent;}, featureArticle); – Mehmet Kurtipek Feb 13 '19 at 22:22
1
This is an existing issue open on puppeteer's git repo.
Track the progress here : https://github.com/GoogleChrome/puppeteer/issues/537