Cant find root elements with RemoteWebDriver.FindElementsByXPath(“/”);

梦想的初衷 提交于 2020-08-08 03:58:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!