问题描述
当我尝试使用
var element = webdriver.FindElementByXPath("/");
或
var elements = webdriver.FindElementsByXPath("/");
我希望获得文档的根元素.
I expect to get the root element(s) of my document.
我得到的是带有此消息的 InvalidSelectorException.
What I get instead is an InvalidSelectorException with this message.
"无效选择器:xpath表达式"/"的结果是:[objectHTML 文档].它应该是一个元素.(会话信息:chrome=50.0.2661.102)(驱动程序信息:chromedriver=2.21.371459(36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),平台=Windows NT 10.0x86_64)"
为什么?是否有某种方法可以获取 HTMLDocument
对象或我最初想要的根元素?
Why? Is there some way of getting that HTMLDocument
object or as I initially wanted, the root element(s)?
xpath /
不是根元素(而不是文档)吗?
Isnt the xpath /
the root element (instead of document)?
推荐答案
/
引用根节点,即文档节点.并且正如方法名称所暗示的那样,它只能返回元素,而不是任意类型的节点.
/
references root node, i.e the document node. And as the method name suggests, it only capable of returning element, not arbitrary kind of node.
要获取文档的根元素,可以使用 /*
代替:
To get root element of the document, you can use /*
instead :
var element = webdriver.FindElementByXPath("/*");
这篇关于无法使用 RemoteWebDriver.FindElementsByXPath("/") 找到根元素;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!