user3911239
user3911239

Reputation: 21

xpath not finding text inside <div><span>

.//*[contains(text(), "Apply")]
<input type="hidden" value="false" name="needsValidation"/>
<input type="hidden" value="false" name="fullValidationPerformed"/>
<div class="loadingBox hidden">
<div class="paneContent">
<div class="topButtons">
    <div class="rightSide">
    <div id="saveChangesButton" class="majorButton">
        <div>
            <div>
                <div>
                    <span class="hidden"/>
                    Apply Changes
                    <span class="down"/>
                </div>
            </div>
        </div>
    </div>
</div>

Why is it that the xpath string I created doesn't find "Apply" here? It appears that my xpath statement only fails when the text I want to find is inside a "span" tag inside a "div" tag like this.

Can someone help me understand what I'm missing here please?

Upvotes: 2

Views: 2373

Answers (1)

Michael Kay
Michael Kay

Reputation: 163322

The reason that contains(text(), 'Apply') does not work is that the element has several text nodes, and the contains function in XPath 1.0 ignores all but the first. (XPath 2.0 would give you an error if there is more than one).

If you want to get an element whose string value contains 'Apply' without also returning its ancestors, the simplest way is to get the last element containing this string:

(//*[contains(., 'Apply')])[last()]

Upvotes: 3

Related Questions