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

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


Because $x() returns an array of HTML or XML elements matching the given XPath expression. It is a shortcut for document.evaluate().If you want to get exact element, just get it by position out of the array

$x(element)[0]

This may be helpful: https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#xpath

If you want to print(or do any other stuff) all text elements found by locator in console - you can just call forEach function:

$x('//a/text()').forEach(function(el){console.log(el)})$x('//a/text()').forEach(el => console.log(el))


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)