Question

so this is an xPath expression which runs fine in a Firefox userscript but fails in a (native) chrome userscript:

var nodesSnapshot = document.evaluate("//a[@class='inline-object' and .[contains(@href,'test.com')] ]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );
//just the string:
"//a[@class='inline-object' and .[contains(@href,'test.com')] ]"

The error message: Uncaught SyntaxError: An invalid or illegal string was specified.

I've tried several things (e.g. extra brackets), googling and searching on stackoverflow without success. I also tried:

"//a[@class='inline-object action-card' and .[@href = 'test.com']]"

which also did not work. Is there someone who can explain this and correct the code?
Thanks in advance

Edit: More important information: The problem seems to be the 'current node' (the dot) in the 'and statement'.

2nd Edit: Here's a test-node: <a href="test.com" class="inline-object"> text of the testnode </a>

Was it helpful?

Solution

XPath expressions like .[true()] are actually illegal (at least in XPath 1.0). Predicates are only allowed

You should simply follow @Ross Patterson's suggestion and write //a[@class='inline-object' and contains(@href,'test.com')]. Or, alternatively but more convoluted:

  • self::node()[contains(@href,'test.com')] or
  • self::*[contains(@href,'test.com')] or
  • (.)[contains(@href,'test.com')]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top