yo3hcv
yo3hcv

Reputation: 1669

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

Upvotes: 3

Views: 812

Answers (2)

Ishita Shah
Ishita Shah

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

Andersson
Andersson

Reputation: 52685

Try below XPath to get required poutput:

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

Upvotes: 2

Related Questions