RB49
RB49

Reputation: 137

Selenium - How to find an object without it having and Id or a Name?

I have recently started using Selenium Webdriver for automation. The webpage I'm trying to automate is in CSS. So what I'm try to achieve is click on a dropdown "Admin", which will then show a list. And select one of the options "User Access" from that list.

Now in the page source, this particular dropdown "Admin" does not have an ID or a Name. Below is the code for reference:

<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
        Admin<span class="caret"></span>
    </a>
    <ul class="dropdown-menu" role="menu">

        <li>
            <a href="usersAdmin">
                Users Admin
            </a>
        </li>

        <li>
            <a href="userAccess">
                User Access Admin
            </a>
        </li>

        <li>
            <a href="#">
                Email Object Admin
            </a>
        </li>

    </ul>
</li>

Now I want to select the User Access Admin value from the dropdown? I'm trying to use the findElement method to identify that object, but since it doesn't have an ID or a name, I'm not able to do that successfully. What would be a suitable method to do this? I'm using Java for coding.

Upvotes: 1

Views: 1904

Answers (3)

drkthng
drkthng

Reputation: 6929

In your case if you want to open the dropdown and choose the "Users Admin" option, then try the following:

// find the dropdown and open it
driver.findElement(By.linkText("Admin")).click();

// find the interesting element and select it via a click
driver.findElement(By.linkText("Users Admin")).click();

Upvotes: 1

Jamie Rees
Jamie Rees

Reputation: 8183

You could do this:

Java

WebElement element = driver.findElement(By.linkText("Users Admin"));

C#

var element = driver.FindElement(By.Name("Users Admin"));

Python

from selenium.webdriver.common.by import By
element = driver.find_element(By.NAME, "Users Admin")

Upvotes: 2

Saifur
Saifur

Reputation: 16201

Should the following css be sufficient?

a[href='userAccess']

Upvotes: 1

Related Questions