Search code examples
google-chromexpath

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


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?


Solution

  • 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://developer.chrome.com/docs/devtools/console/utilities#xpath-function

    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))