Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript, XPath, select next text based on search text

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

like image 428
orfruit Avatar asked Mar 06 '26 22:03

orfruit


1 Answers

Try below XPath to get required poutput:

//div[label="Factory Lead Time:"]/following-sibling::div/text()
like image 164
Andersson Avatar answered Mar 09 '26 12:03

Andersson