Search code examples
javaseleniumxpathdrop-down-menudropdown

How to select a DropDown from a list but HTML don't have select Tag using Selenium Java


I am new to selenium and i would like to perform a simple task I want to select a drop-down from a tab and i have used "http://www.spicejet.com/" as an Reference. There is a Tab "Add-On" in webpage spicejet.com and it contains Drop Down value i would like to select any one value from the list. HTML code don't have select tag so Select class not giving me appropriate result.

Here is the HTML code:

<a href="javascript:void(0);" id="highlight-addons" class="">Add-Ons<span class="rightarrowclass">&nbsp;</span><span class="rightarrowclass">&nbsp;</span></a>
<li><a href="SpiceClubMembershipOffer.aspx">SpiceClub Membership Offer</a></li>
<li id="ctl00_lblSpiceClublink">
                                                <a id="ctl00_lblSpiceClub" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$lblSpiceClub", "", false, "", "SpiceClub.aspx", false, true))'>SpiceCash/SpiceClub</a></li>
<li><a href="http://book.spicejet.com/SpiceMoneyTopup.aspx">SpiceCash Topup </a></li>
<a href="http://book.spicejet.com/RetrieveBooking.aspx?AddSeat=true">SpiceMax </a>

Please help me let me know how to move forward.


Solution

  • To select a value from the DropDown, you have to first Mouse Hover the WebElement ADD-ONS, then to select SpiceClub Membership Offer you can use the following code block :

    WebElement elem = driver.findElement(By.xpath("//a[@id='highlight-addons']"));
    Actions action = new Actions(driver);
    action.moveToElement(elem).perform();
    List<WebElement> items = driver.findElements(By.xpath("//ul[@class='add-ons-tab']/li/a"));
    for(WebElement myitem:items)
    {
        if(myitem.getAttribute("innerHTML").contains("Membership"))
        {
            myitem.click();
            break;
        }
    }