Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting value from "bootstrap dropdown" in selenium webdriver?

Tool: Selenium Webdriver

I am trying to click on a specific value from a drop-down list. I can do the same via following:

HTML snippet: via "Select" attribute

<select class="regionpicker-state span3 ng-valid ng-dirty" ng-change="updateSelection()" ng-options="a.code as a.name for a in state" ng-model="selectedState">
  <option class="" value="">Select</option>
  <option value="0">Karnataka</option>
  <option value="1">Tamil Nadu</option>
</select>

Option 1 for automation: via "Select" attribute

 @FindBy(xpath = ".//*[@id='organization_chosen']/a/span")
   public static WebElement organizationExpand;
 new Select(organizationExpand).selectByVisibleText("abc");

But, the above "option 1" code works if the code has "Select" attribute.

What if the devs have coded dropdowns by using "bootstrap dropdown" approach?

HTML snippet: via "bootstrap dropdown" approach:

<select id="organization" class="focused" data-placeholder="Choose a organization..." name="organization" style="display: none;">
   <div id="organization_chosen" class="chosen-container chosen-container-single chosen-container-active" style="width: 220px;" title="">
      <a class="chosen-single chosen-default" tabindex="-1">
      <div class="chosen-drop">
         <div class="chosen-search">
         <ul class="chosen-results">
           <li class="active-result" data-option-array-index="1" style="">Demo</li>
           <li class="active-result" data-option-array-index="2" style="">abc</li>
           <li class="active-result" data-option-array-index="3" style="">def</li>
           <li class="active-result" data-option-array-index="4" style="">xyz</li>
         </ul>
like image 330
Yagnesh Shah Avatar asked Feb 22 '26 05:02

Yagnesh Shah


1 Answers

Here is the solution I found for bootstrap dropdown menu:

@FindBy(xpath =".//*[@id='organizationType_chosen']/a/span") //this xpath is referring the path where we can click on expanding the drop-down
    public static WebElement orgTypeExpand;
@FindBy(xpath =".//*[@id='organizationType_chosen']/div/ul") //this xpath is referring to exact location to the <ul> attribute which will be visible in HTML DOM structure only after you expand the drop-down
    public static WebElement orgTypeDropDown;

public static void organizationTypeBootStrapDropDownValueSelection(String organizationTypeValue)//consider parameter passed for 'organizationTypeValue' is 'abc' value from drop-down
{
    int flag=0;
    orgTypeExpand.click();
    //Do a implicit wait here until the dropdown menu expands as the HTML Dom structure dynamically gets updated with each dropdown values once the drop-down expands. 

    List<WebElement> organizationType = orgTypeDropDown.findElements(By.className("active-result")); //retrieve all values from the drop-down. Note: This pageobject 'orgTypeDropDown' has slight different xpath than 'orgTypeExpand'
    flag=0;
    for (WebElement orgType : organizationType) //For each value retrieved check if it matches with our expected value to be clicked 'organizationTypeValue'
    {   
        if (orgType.getText().equals(organizationTypeValue)) 
        {
            orgType.click();
            flag=1;
            break;
        }
    }
    Assert.assertTrue(flag==1, "OrganizationType '" + organizationTypeValue + "' passed as parameter is a mismatch with values available in organization Type drop-down...");//This will assist you in making sure if the drop-down value was clicked or not. It fails if the value is not selected & stops the script execution
}
like image 105
Yagnesh Shah Avatar answered Feb 24 '26 14:02

Yagnesh Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!