基本上,我试图使用Selenium的Xpath处理以下HTML:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<a>Public Profile</a>
</html>我使用的选择器如下:
//a[text() = 'Public Profile']看起来很简单,但是,根据Selenium,它返回0匹配。我也尝试过在线xpath测试器:
它也不返回任何结果。奇怪的是当我移除
xmlns="http://www.w3.org/1999/xhtml“
-attribute,它发现匹配没有问题。
有人能向我解释为什么xmlns标记会导致Xpath查询失败吗?
在sidenote上,我的C# selenium-xpath查询看起来如下:
Driver.FindElement(By.XPath("//a[text() = 'Public Profile']"))编辑:我找到的一个链接,它很好地解释了正在发生的事情:
发布于 2015-08-26 16:53:37
就XML/XPath处理而言,xmlns="http://www.w3.org/1999/xhtml"部件将html元素放入XML命名空间。
a元素继承该命名空间。而//a[text() = 'Public Profile'] XPath表达式将只匹配一个未命名空间的a元素。
//a[namespace-uri()='http://www.w3.org/1999/xhtml'][text() = 'Public Profile']是使其匹配的一种方法。
//*[name()='a'][text() = 'Public Profile']是另一种方式。
//*[text() = 'Public Profile']是另一种方式(假设您已经知道这将得到您想要的a元素,而不是其他元素)。
https://stackoverflow.com/questions/32232299
复制相似问题