I have the following Xpath expression:
//*[not(input)][ends-with(@*, 'Copyright')]
I expect it to give me all elements - except input - with any attribute value which ends with "Copyright".
I execute it in the Selenium 2 Java API with webDriver.findElements(By.xpath(expression))
and get the following error:
The expression is not a legal expression
But these expressions work without trouble:
//*[not(input)][starts-with(@*, 'Copyright')]
//*[ends-with(@*, 'Copyright')]
Any ideas?
Most likely explanation is that you are using an XPath 1.0 processors. The ends-with() function requires XPath 2.0 support.
I don't know Selenium but if
//*[not(input)][starts-with(@*, 'Copyright')]
is parsed successfully and if additionally the XPath 2.0 functionends-with
is supported then I don't see any reason why//*[not(input)][ends-with(@*, 'Copyright')]
is not accepted as a legal expression. Your verbal description however sounds as if you want//*[not(self::input)][@*[ends-with(., 'Copyright')]]
.//*[not(input)]
selects any elements not having any input child element while//*[not(self::input)]
selects any elements not being themselvesinput
elements. As for comparing[@*[ends-with(., 'Copyright')]]
with what you have, my suggestion is true as long as there is any attribute node which ends with 'Copyright' while your test would only work if there is a single attribute which ends with 'Copyright', as ends-with http://www.w3.org/TR/xquery-operators/#func-ends-with allow a sequence with a single item as its first argument or an empty sequence but not several items.There are a few issues here:
ends-with()
is a standard XPath 2.0 function only, so the chances are you are using an XPath 1.0 engine and it correctly raises an error because it doesn't know about a function calledends-with()
.Even if you are working with an XPath 2.0 processor, the expression
ends-with(@*, 'Copyright')
results in error in the general case, because theends-with()
function is defined to accept atmost a single string (xs:string?
) as both of its operands -- however@*
produces a sequence of more than one string in the case when the element has more than one attribute.//*[not(input)]
doesn't mean "select all elements that are not namedinput
. The real meaning is: "Select all elements that dont have a child element named "input".Solution:
Use this XPath 2.0 expression:
//*[not(self::input)][@*[ends-with(.,'Copyright')]]
In the case of XPath 1.0 use this expression:
....
Here is a short and complete verification of the last XPath expression, using XSLT:
when this transformation is applied on the following XML document:
the wanted, correct result is produced:
In the case of the XML document being in a default namespace:
when applied on this XML document:
the wanted, correct result is produced:
May be small correction with
string-length(.)
Now, it may work.