xpath - 无法使用 RemoteWebDriver.FindElementsByXPath ("/"找到根元素);

标签 xpath selenium-webdriver chrome-web-driver

当我尝试使用时

var element = webdriver.FindElementByXPath("/");

var elements = webdriver.FindElementsByXPath("/");

我希望获得文档的根元素。

我得到的是带有此消息的 InvalidSelectorException。

"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)"

为什么?有没有某种方法可以获取 HTMLDocument 对象或如我最初想要的那样,获取根元素?

xpath / 不是根元素(而不是文档)吗?

最佳答案

/ 引用根节点,即文档节点。正如方法名称所暗示的,它只能返回元素,而不是任意类型的节点。

要获取文档的根元素,您可以使用/*:

var element = webdriver.FindElementByXPath("/*");

关于xpath - 无法使用 RemoteWebDriver.FindElementsByXPath ("/"找到根元素);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37414306/

相关文章:

python - 无法对拆卸中的异常使用react

java - 使用 selenium webdriver 循环遍历表

javascript - Protractor-加载主页后无法定位元素

python - 在 python 中使用 selenium webdriver 查找具有显式等待的元素

java - Selenium WebDriver 支持机器人框架吗?

python - 带 Selenium 的 Instagram 登录脚本,无法执行 .send_keys ('test' )

c# - ChromeDriver 显示丢失的 UI 共享上下文

javascript - 使用 XPath 在 DOM 中搜索多个相同字符串

java - 无法使用 Selenium webdriver Java 单击按钮

javascript - 为什么我的 NodeJS 脚本在 fs.readFile 和 fs.appendFile 处理大量文件时陷入困境。