1

For example the document.querySelectorAll works with CSS selectors, but how can I use XPath expressions instead?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
microwth
  • 1,016
  • 1
  • 14
  • 27

2 Answers2

4

They just released this feature :)

page.xpath(expression)

expression <string> Expression to evaluate

returns:<Promise<?ElementHandle>> Promise which resolves to ElementHandle pointing to the page element.

check it out here

Community
  • 1
  • 1
  • 1
    This 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

Anil
  • 3,722
  • 2
  • 24
  • 49
microwth
  • 1,016
  • 1
  • 14
  • 27