Reputation: 1299
Basically I'm trying to process the following HTML using the Xpath of Selenium:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<a>Public Profile</a>
</html>
I'm using the following selector:
//a[text() = 'Public Profile']
Seems simple enough, however, according to Selenium it returns 0 matches. I have tried in the online xpath tester as well:
and it doesn't return any results neither. The strange thing is that when I remove the
xmlns="http://www.w3.org/1999/xhtml"
-attribute it finds the match without a problem.
Can anyone explain to me why the xmlns tag makes the Xpath query fail?
On a sidenote, my C# selenium-xpath query looks the following:
Driver.FindElement(By.XPath("//a[text() = 'Public Profile']"))
EDIT: A link I found which explains what's going on nicely:
Upvotes: 3
Views: 3319
Reputation: 88066
As far as XML/XPath processing goes, the xmlns="http://www.w3.org/1999/xhtml"
part puts the html
element into an XML namespace.
And the a
element inherits that namespace. And the //a[text() = 'Public Profile']
XPath expression will only match an un-namespaced a
element.
//a[namespace-uri()='http://www.w3.org/1999/xhtml'][text() = 'Public Profile']
is one way to make it match.
//*[name()='a'][text() = 'Public Profile']
is another way.
And //*[text() = 'Public Profile']
is yet another way (assuming you already know that’ll get the a
element you want, and not some other element).
Upvotes: 3