Learner
Learner

Reputation: 157

XPath to select first element in a dropdown list

The list in the drop down which i'm working on, is not static. Values in current dropdown are : Lock1, Lock2, Lock3, Lock4. As these are dynamic values, i want to write my own xpath according to its position in drop down list. I want to select Lock1.

<div class="dropdown open">
    <button class="btn btn-white dropdown-toggle btn-block" type="button">
        <!-- react-text: 62 -->
        Lock1
        <!-- /react-text -->
        <span class="glyphicon glyphicon-chevron-up"/>
    </button>
    <div class="dropdown-menu">
        <div class="m-l-xs m-r-xs">
            <div class="search">
                <input type="text" placeholder="search"/>
                <i class="icon icon-search"/>
            </div>
        </div>
        <ul class="dropdown-list">
            <li class="active">
                <a title="Lock1">Lock1</a>
            </li>
            <li class="">
                <a title="Lock2">Lock2</a>
            </li>
            <li class="">
                <a title="Lock3">Lock3</a>
            </li>
            <li class="">
                <a title="Lock4">Lock4</a>
            </li>
        </ul>
    </div>
</div>

Upvotes: 2

Views: 4505

Answers (2)

renato vitolo
renato vitolo

Reputation: 1754

(//ul[@class='dropdown-list']/li[@class='active']/a)[1]

or

(//div[@class='dropdown-menu']/ul[@class='dropdown-list']/li[@class='active']/a)[1]

References:

XPath Get first element of subset

XPath: Select first element with a specific attribute

Upvotes: 3

Michael Kohl
Michael Kohl

Reputation: 66837

//ul/li[1] returns Lock1, is that what you're after?

You might want to be a bit more specific with ul or li classes, but I'll leave that up to you.

PS: Next time you ask somebody to write an XPath expression for you, please make sure your input is valid (you're missing a closing </div>).

Upvotes: 0

Related Questions