Ketan Deopujari
Ketan Deopujari

Reputation: 113

Selenium Webdriver (Java) Bootstrap dropdown menu access

I have a webpage that contains a bootstrap navigation bar. This navigation bar has three dropdown menus.

I want to access one of these dropdown menus and select one option from it by using Selenium Webdriver in Java.

The problem is that all the three dropdown menus have same HTML and there is no unique identifier to distinguish them from each other.

Can anyone guide me on how to access these dropdown menus in selenium webdriver-Java. Below is the HTML snippet,

    <div aria-expanded="false" id="navbar" class="navbar-collapse collapse secondnav">
        <ul class="nav navbar-nav">

            <!-- <li><a href="#">Forms Grid</a>
            </li> -->
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Reports <span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">                    
                    <li><a href="#" id="abcd">Choice 1</a></li>                     
                    <li><a href="#" id="pqrst">Choice 2</a></li>                        
                    <li><a href="#" id="uvwxy">Choice 3</a></li>                                                                    
                </ul>
            </li>

            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Admissions <span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">
                    <li><a id="admit123">Admit new guy </a></li>                            
                    <li><a href="enrollExisting">Enroll Existing admit </a></li>                            
                </ul>
            </li>               
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Findings<span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">              
                    <li class="dropdown-submenu"><a tabindex="-1" href="#">Samples</a>
                        <ul class="dropdown-menu">
                            <li><a href="#" tabindex="-1">Add</a>
                            </li>
                            <li><a href="#" tabindex="-1">Update</a>
                            </li>
                            <li><a href="#" tabindex="-1">Update Off Site</a>
                            </li>
                        </ul>
                    </li>


                </ul>
            </li>
        </ul>       
    </div>

Upvotes: 2

Views: 2458

Answers (1)

alecxe
alecxe

Reputation: 473763

For starters, you can get a specific dropdown by index:

List<WebElement> menus = driver.findElements(By.cssSelector("div#navbar  ul li.dropdown"));

WebElement desiredMenu = menus.get(0);
desiredMenu.click();

Then, you'll have multiple ways to locate the menu options. For instance, by text:

WebElement choice1 = desiredMenu.findElement(By.linkText("Choice 1"));
choice1.click();

Upvotes: 1

Related Questions