question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get text content on XPath expression

See original GitHub issue

Hi, I have like this.

var content = await page.$x("//span[@class='property-number']");

How to get text content inside that expression?

I already try with this, but not working.

var content = await page.evaluate(el => el.innerHTML, await page.$x("//span[@class='property-number']"));

Any help are appreciated.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

41reactions
aslushnikovcommented, Jan 19, 2018

@sonyarianto the following should work:

const browser = await puppeteer.launch();

const page = await browser.newPage();
await page.goto('https://example.com/');

const title = await page.$x("//h1");
let text = await page.evaluate(h1 => h1.textContent, title[0]);
console.log(text)

await browser.close();
6reactions
ashleyR94commented, Jan 18, 2018

This is the closest i have been able to get with my understanding `const browser = await puppeteer.launch();

const page = await browser.newPage(); await page.goto(‘https://example.com/’);

const title = await page.$x(“//h1”); let text = await title[0].getProperty(‘textContent’); console.log(text)

await browser.close();`

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get text content of an HTML element using XPath?
In the DOM Level 3 word you would select the p elements with e.g. //div[a[contains(., "Add to cart")]]/p and then access the textContent...
Read more >
XPath in Selenium: How to Find & Write? (Text, Contains, ...
Contains () is a method used in XPath expression. It is used when the value of any attribute changes dynamically, for example, login...
Read more >
How does XPath text work? | Examples
XPath text () function is a built-in function of the Selenium web driver that locates items based on their text. It aids in...
Read more >
Introduction to using XPath in JavaScript - MDN Web Docs
This document describes the interface for using XPath in JavaScript internally, in extensions, and from websites. Mozilla implements a fair ...
Read more >
Selecting content on a web page with XPath
XPath Expressions · XPath always assumes structured data. · Navigating through the HTML node tree using XPath · Navigating through a webpage with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found