0

For the below code

<li class="user ui-menu-item" role="presentation">
   <a id="ui-id-52" class="ui-corner-all" tabindex="-1">
   <em>User:</em>
       Staff User
   </a>
</li>

This is the scenario:

There is a text field, in which I enter the name Staff, with that the related values for the staff are displayed in the dynamic list box, in the above scenario the id is dynamically generated, and when I tried to select the value by class, it is same for all the elements.

I want an xpath expression to select the first available options in the list. I tried in many ways like with contains and starts-with, but no use. Please let me know your valuable suggestion.

Thanks in Advance Shiva.

Mark Rowlands
  • 4,993
  • 2
  • 25
  • 40
Shiva Challa
  • 55
  • 4
  • 11

4 Answers4

0

Did you try forming a List and then just directly using the first element of that List?

List<WebElement> list = driver.findElements(By.xpath("//li[@class(contains, 'user')]";
list[0].getText();

I'm still not 100% percentage sure what you want to do with the element once you have found it though. It would probably be better to form the List, then iterate over that list and perform whatever action you require per element.

Mark Rowlands
  • 4,993
  • 2
  • 25
  • 40
0

Use this xpath

 "(.//a[@class='ui-corner-all'])[1]"

source: XPath query to get nth instance of an element

Community
  • 1
  • 1
cegprakash
  • 2,317
  • 26
  • 52
0

Use the xpath. It should work

//li[@role = 'presentation']//a[1]

Vinay
  • 558
  • 3
  • 11
0

I think this should work

WebElement ele = webdriver.findElement(By.Xpath("//li[@class='user ui-menu-item'][1]"))
Harshavardhan Konakanchi
  • 3,956
  • 6
  • 30
  • 52