11

I'm new to selenium and HTML. I want to perform click operation for the elements "Users" and "Admins" as one after another. Below mentioned is the HTML code, unfortunately I can't modify it.

<ul id="nav">
<li class="">
 <a class="mainmenu" title="List of Users" menuid="nav_0">
 <span class="title">Users</span>
 <span class="arrow"/>
 </a>
 <ul class="sub-menu">
 <li>
 <a class="mainmenu" title="List of Admins" menuid="nav_0-sub_0">Admins</a>
 </li>
 </ul>
</li>
<li class="">
 <a class="mainmenu" title="List of Contents" menuid="nav_1">
 <span class="title">Contents</span>
 </a>
</li>
<li class="">
 <a class="mainmenu" title="Repos" menuid="nav_2">
 <span class="title">Repository</span>
 </a>
</li> 

I have used XPath like this:

.//*[@id='nav']/li[1]/a[contains(@title, 'List of Users'] 

Any other ways to get the element like matching the text value? Because the list elements order is dynamic, so the current position of the element "Users"(li[1]) might get change later.

Bence Kaulics
1,00712 silver badges22 bronze badges
asked Sep 21, 2015 at 10:43

4 Answers 4

2

As per given description, it seems that list of users will be maintained in HTML's List Item having common title=List of Users. So you can use something like below code to click on dynamic list items(here, users).

List<WebElement> Userlist = driver.findElements(By.cssSelector("a[title='List of Users']");
for(WebElement ulist : Userlist)
{
 //Do your action, e.g. click() with each user name.
}
Bence Kaulics
1,00712 silver badges22 bronze badges
answered Sep 21, 2015 at 11:00
1
  • I find it hilarious that the accepted answer shows how to do it with a CSS locator when the OP explicitly asked for an XPath solution and there are valid XPath answers... Commented Jun 3, 2019 at 9:17
3

You can try creating xpath with text() like these:

  1. //span[contains(text(),'Users')]
  2. //span[contains(text(),'Contents')]
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Sep 23, 2015 at 6:36
2
  • It failed to pick element by using this. Commented Sep 23, 2015 at 7:08
  • 2
    Pankaj Kumar Katiyar/demouser123 is missing the closing bracket ]. //span[contains(text(),'Users')] //*[contains(text(),'Users')] //span[text()='Users'] //*[text()='Users'] //a[contains(text(),'Admins')] //*[contains(text(),'Admins')] //a[text()='Admins'] //*[text()='Admins'] any of these should work Commented Sep 17, 2018 at 19:48
2

To avoid the li[1], just use this XPath. It will still work, even if one of the li elements swap their locations in the html file.

//*[@id='nav']/li/a[contains(@title,'List of Users')]

If you only have one <a> element on the page, you could avoid the li part

//a[contains(@title,'List of Users')]
Bence Kaulics
1,00712 silver badges22 bronze badges
answered Nov 30, 2018 at 12:03
1
//*[@id='nav']/li[1]/a[contains(@title, 'List of Users'] 

You missed ) at the last ], it should be like below:

//*[@id='nav']/li[1]/a[contains(@title, 'List of Users')] 
Bence Kaulics
1,00712 silver badges22 bronze badges
answered May 31, 2019 at 10:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.