-1

I have copied the xpath Chrome returns for a certain node

//*[@id="__w2_cEfEsuQ_list_item"]/div/div[2]/div/div[1]/div[2]/a[1]

Executing this in the console of Chrome returns

[a]
    0: a
    length: 1
    __proto__: Array(0)

0 can be expanded and contains a lot of subnodes (I removed most of them because SO complained about too much code in my question), like this:

accessKey: ""
tagName: "A"
target: ""
text: "38 Answers"
textContent: "38 Answers"
title: ""
translate: true
type: ""
username: ""

I want an xpath that returns the textContent and text nodes. I have tried different variations on //*[@id="__w2_cEfEsuQ_list_item"]/div/div[2]/div/div[1]/div[2]/a[1]/text() but none has worked. How do I do?

  • 2
    IIRC this is only possible using XPath-2.0. If you're stuck with XPath-1.0, you're out of luck this way. – zx485 Sep 24 '18 at 19:29
  • 1
    What version of Xpath is supported by the console in Chrome? –  Sep 25 '18 at 06:11

1 Answers1

1

The Chrome console features indeed only XPath 1.0.
Nonetheless, getting the text() should work as expected.

To Run XPath queries on the current page directly from Chrome's console,

  • Open the console (F12, cmd-opt-J on a Mac), then
  • placing your query inside $x("") and run it.

If you want to print all text elements found by the locator in the console (or do other stuff) just call .forEach

$x('//*[@id="__w2_cEfEsuQ_list_item"]/div/div[2]/div/div[1]/div[2]/a[1]/text()').forEach(function (item) {
    console.log(item);});

or access the elements of the result array directly using the index $x("//p/text()")[0]

wp78de
  • 16,078
  • 6
  • 34
  • 56