2

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>
Learner
  • 157
  • 1
  • 4
  • 13

2 Answers2

3
(//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

Community
  • 1
  • 1
renato vitolo
  • 1,744
  • 11
  • 16
  • When I use your xpaths, I receive an exception: Unable to locate element. However I have edited your given path to: (//ul[@class='dropdown-list']/li[1])[1] and this xpath works.. Thanks for help – Learner Aug 15 '16 at 12:13
  • Probably the exception is raised because none of the `li` elements has `class="active"` in your application. – renato vitolo Aug 15 '16 at 12:41
0

//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>).

Michael Kohl
  • 66,324
  • 14
  • 138
  • 158