有没有办法通过 Xpath 获取元素,包括 Javascript 中的隐藏项目? [英] Is there a way to get element by Xpath including hidden items in Javascript?

查看:22
本文介绍了有没有办法通过 Xpath 获取元素,包括 Javascript 中的隐藏项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 xpath 获取 DOM 元素.我从这个链接得到了答案有没有办法使用 XPath 获取元素Selenium WebDriver 中的 JavaScript?.在隐藏元素(显示:无)的情况下,此函数不会返回确切结果.

I need to get DOM element by xpath. I got the answer from this link Is there a way to get element by XPath using JavaScript in Selenium WebDriver?. This function is not returning the exact results in the case when the element is hiddden(display:none).

在示例中,我试图获取第一个 div 内容,但它显示了第二个 div 元素,它将隐藏元素纳入计数.我还需要在计数中取出隐藏元素.需要这方面的帮助.

In the example, I am trying out to get the first div content, But it is showing second div element it is taking hidden elements in to the count. I need to take hidden elements also in the count. Need help regarding this.

推荐答案

我把你的代码片段放在这里并稍作修改.

I am having your snippet here and modified it slightly.

对我来说它有效,它甚至可以访问隐藏节点的内容并将其添加到结果页面.(Firefox 35.0 浏览器/Debian)

For me it works, it accesses the content even of the hidden node and adds this to the result page. (Firefox 35.0 Browser / Debian)

您是否偶然将显示的 HTML 结果误解为日志输出?

Were you by chance misinterpreting the displayed HTML result as log output?

function getElementByXpath (path) {
  return document.evaluate(path, document, null,
    XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

document.write("<br/>1: [" + 
  getElementByXpath("//html/body/div[1]").innerHTML + "]");
document.write("<br/>2: [" + 
  getElementByXpath("//html/body/div[2]").innerHTML + "]");

<div style="display:none;">First Div</div>
<div>Second Div</div>

这篇关于有没有办法通过 Xpath 获取元素,包括 Javascript 中的隐藏项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆