简体   繁体   中英

Is there a way to get element by Xpath including hidden items in 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).

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.

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)

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM