Javascript,XPath,根据搜索文本选择下一个文本

时间:2018-06-27 12:04:34

标签: javascript xpath

我需要使用XPath通过文本包含(内部文本)搜索节点

<div class="row"> // 1-0-4
    <div class="col-xs-4">
        <label for="FactoryLeadTime">Factory Lead Time:</label>
    </div>
    <div class="col-xs-8">
        4 Weeks
    </div>
</div>

我的JS代码

var xpath = 'label[text()[normalize-space(.)="Factory Lead Time:"]]'; 
//var xpath = "//label[a[contains(., 'Factory Lead Time:')]]/text()";
var res = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);

console.log(res);

由于控制台显示,我不确定搜索是否成功

XPathResultinvalidIteratorState: falseresultType: 4__proto__: XPathResultPrototype { iterateNext: iterateNext(), snapshotItem: snapshotItem(), ANY_TYPE: 0, … } tmp.js:252:1

事实上,我正在尝试根据先前的文本(“工厂交货时间:”)获得“ 4周”的结果。

是否有使用XPath的聪明方法?

预先感谢

2 个答案:

答案 0 :(得分:2)

尝试在XPath下获取所需的poutput:

//div[label="Factory Lead Time:"]/following-sibling::div/text()

答案 1 :(得分:0)

尝试一下,

<label for="FactoryLeadTime">Factory Lead Time:</label>

Xpath: //label[contains(text(),'Factory Lead Time:')]

<div class="col-xs-8">
         4 Weeks
     </div>

Xpath: //div[normalize-space()='4 Weeks']