问题
When I try to use
var element = webdriver.FindElementByXPath("/");
or
var elements = webdriver.FindElementsByXPath("/");
I expect to get the root element(s) of my document.
What I get instead is an InvalidSelectorException with this message.
"invalid selector: The result of the xpath expression "/" is: [object HTMLDocument]. It should be an element. (Session info: chrome=50.0.2661.102) (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 10.0 x86_64)"
Why? Is there some way of getting that HTMLDocument
object or as I initially wanted, the root element(s)?
Isnt the xpath /
the root element (instead of document)?
回答1:
/
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("/*");
来源:https://stackoverflow.com/questions/37414306/cant-find-root-elements-with-remotewebdriver-findelementsbyxpath