1

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>

user2305193
  • 2,079
  • 18
  • 39
  • 2
    Why note merely `//a[@class='inline-object' and contains(@href,'test.com')]`? – Ross Patterson Dec 30 '13 at 00:06
  • Seems also to cause a 'Uncaught SyntaxError: An invalid or illegal string was specified.' error :( The only reason I wrote it that way is to specify that I want the current node to match the and statement not any other (i.e. when using //a instead of . ) – user2305193 Dec 30 '13 at 00:09
  • The expression suggested by @RossPatterson is equivalent to yours. I tried it on Chromium and seems to work fine. – Carlo Cannas Dec 30 '13 at 00:56
  • I still can't make mine work (if you suggested that), but the expression by @RossPatterson is working, thanks! – user2305193 Dec 30 '13 at 01:14

1 Answers1

2

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')]
nwellnhof
  • 32,319
  • 7
  • 89
  • 113
  • Thanks, I must have messed up my string, it was somewhat longer and more complicated. Apologies to Ross Patterson. I found little tutorials on the 'and'/more complex statements, I followed http://www.w3schools.com/xpath/xpath_syntax.asp but it's clearer now. THANKS! – user2305193 Dec 30 '13 at 01:05
  • For future reference, W3Schools is a very poor resource. It's information is often incorrect. – Ross Patterson Dec 30 '13 at 13:12