Reputation: 1669
I need to search for a node using XPath by text contains (inner text)
<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>
My JS code
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);
I am not sure if the search succeed since console shows
XPathResultinvalidIteratorState: falseresultType: 4__proto__: XPathResultPrototype { iterateNext: iterateNext(), snapshotItem: snapshotItem(), ANY_TYPE: 0, … } tmp.js:252:1
In fact i am trying to obtain the result which is "4 Weeks" based on previous text which is "Factory Lead Time:"
Is there a clever way to do this with XPath?
Thanks in advance
Upvotes: 3
Views: 812
Reputation: 4035
Try this,
<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']
Upvotes: 0
Reputation: 52685
Try below XPath to get required poutput:
//div[label="Factory Lead Time:"]/following-sibling::div/text()
Upvotes: 2