Why doesn't google chrome print the text() of my XPath query?

Viewed 14816

I am trying to extract the texts of an xpath query, $x('//td[@class="first"]/a/text()') in Chrome but then when I run this command I see text as opposed to the actual text value of the anchor links.

When I run s.toString() I am seeing [object Text],[object Text],[object Text],[object Text]....

How can I get their string value in the xpath?

3 Answers

Use the following command under these circumstances:
- The element's text is stored in data.
- You want to trim leading and trailing whitespace from the data.
- You want to easily copy paste the output from the console delimited by break-line.

console.log($x(<pathToElement>/text()').map(function(el){return el.data.trim()}).join("\n"))

You can use .map(a=>a.textContent) to extract your need.

$x('//td[@class="first"]/a/text()').map(a=>a.textContent)
Related