How to handle drop downs using Selenium WebDriver in Java

+1 vote

I'm trying to select certain options from a drop-down menu. The source code is:

<div class="multi ng-isolate-scope" isteven-multi-select="" input-model="genders" output-model="filter.genders" button-label="name" item-label="name" tick-property="ticked" on-select-all="search()" on-select-none="search()" on-item-click="search()" output-properties="id">
<span class="multiSelect inlineBlock buttonClicked">
<button id="" type="button" ng-click="toggleCheckboxes( $event ); refreshSelectedItems(); refreshButton(); prepareGrouping; prepareIndex();" ng-bind-html="varButtonLabel" ng-disabled="disable-button" class="ng-binding">None Selected<span class="caret"></span></button>
  <div
    class="checkboxLayer show">
    <!-- ngIf: helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset -->
    <div class="helperContainer ng-scope" ng-if="helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset ">
      <!-- ngIf: helperStatus.all || helperStatus.none || helperStatus.reset -->
      <div class="line ng-scope" ng-if="helperStatus.all || helperStatus.none || helperStatus.reset ">
        <!-- ngIf: helperStatus.all --><button type="button" class="helperButton ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.all" ng-click="select( 'all', $event );" ng-bind-html="lang.selectAll">✓&nbsp;&nbsp;Select All</button>
        <!-- end ngIf: helperStatus.all -->
        <!-- ngIf: helperStatus.none --><button type="button" class="helperButton ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.none" ng-click="select( 'none', $event );" ng-bind-html="lang.selectNone">×&nbsp;&nbsp;Select None</button>
        <!-- end ngIf: helperStatus.none -->
        <!-- ngIf: helperStatus.reset --><button type="button" class="helperButton reset ng-binding ng-scope" ng-disabled="isDisabled" ng-if="helperStatus.reset" ng-click="select( 'reset', $event );" ng-bind-html="lang.reset">↶&nbsp;&nbsp;Reset</button>
        <!-- end ngIf: helperStatus.reset -->
      </div>
      <!-- end ngIf: helperStatus.all || helperStatus.none || helperStatus.reset -->
      <!-- ngIf: helperStatus.filter -->
      <div class="line ng-scope" style="position:relative" ng-if="helperStatus.filter"><input placeholder="Search..." type="text" ng-click="select( 'filter', $event )" ng-model="inputLabel.labelFilter" ng-change="searchChanged()" class="inputFilter ng-pristine ng-valid ng-touched" style=""><button type="button" class="clearButton"
          ng-click="clearClicked( $event )">×</button> </div>
      <!-- end ngIf: helperStatus.filter -->
    </div>
    <!-- end ngIf: helperStatus.filter || helperStatus.all || helperStatus.none || helperStatus.reset -->
    <div class="checkBoxContainer">
      <!-- ngRepeat: item in filteredModel | filter:removeGroupEndMarker -->
      <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}"
        ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );" style="">
        <!-- ngRepeat: i in numberToArray( item[ spacingProperty ] ) track by $index -->
        <div class="acol"><label><input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )"><span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">&nbsp;Male</span></label></div>
        <!-- ngIf: item[ groupProperty ] !== true && item[ tickProperty ] === true -->
      </div>
      <!-- end ngRepeat: item in filteredModel | filter:removeGroupEndMarker -->
      <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}"
        ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );">
        <!-- ngRepeat: i in numberToArray( item[ spacingProperty ] ) track by $index -->
        <div class="acol"><label><input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )"><span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">&nbsp;Female</span></label></div>
        <!-- ngIf: item[ groupProperty ] !== true && item[ tickProperty ] === true -->
      </div>
      <!-- end ngRepeat: item in filteredModel | filter:removeGroupEndMarker -->
      <div ng-repeat="item in filteredModel | filter:removeGroupEndMarker" class="multiSelectItem ng-scope vertical" ng-class="{selected: item[ tickProperty ], horizontal: orientationH, vertical: orientationV, multiSelectGroup:item[ groupProperty ], disabled:itemIsDisabled( item )}"
        ng-click="syncItems( item, $event, $index );" ng-mouseleave="removeFocusStyle( tabIndex );">
        <!-- ngRepeat: i in numberToArray( item[ spacingProperty ] ) track by $index -->
        <div class="acol"><label><input class="checkbox focusable" type="checkbox" ng-disabled="itemIsDisabled( item )" ng-checked="item[ tickProperty ]" ng-click="syncItems( item, $event, $index )"><span ng-class="{disabled:itemIsDisabled( item )}" ng-bind-html="writeLabel( item, 'itemLabel' )" class="ng-binding">&nbsp;Not Recorded</span></label></div>
        <!-- ngIf: item[ groupProperty ] !== true && item[ tickProperty ] === true -->
      </div>
      <!-- end ngRepeat: item in filteredModel | filter:removeGroupEndMarker -->
    </div>
</div>
</span>
</div>

After doing some research, I realized they all use "By.id", but the source code doesn't have 'id', so I used XPath instead but still it does not work. 

Select gender = new Select(driver.findElement(By.xpath("//*[@id=\"offenderListView\"]/div/div[1]/ul/form/div[2]/span/button")));
gender.selectByVisibleText("Male");
Mar 27, 2018 in Selenium by code_ninja
• 6,300 points
8,303 views

1 answer to this question.

0 votes

First, find an XPath which will return all values in the drop-down. Then using List, you can select any value.

List<WebElement> myElements = driver.findElements(mySelector);
   for(WebElement e : myElements) {
     if(e.getText().equalsIgnoreCase("Your_Desire_Value")) {
         e.click();
     }

Note:- click on the drop-down first using simple click operation and then use the above code. You may you also need to apply focus if the value is present deep below in the list.

answered Mar 27, 2018 by nsv999
• 5,500 points

Related Questions In Selenium

+2 votes
3 answers

How can we handle authentication popup in Selenium WebDriver using Java

1) By passing user credentials in URL. String ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
29,017 views
+10 votes
17 answers

How to automate gmail login process using selenium webdriver in java?

Check the below code: Here is the working ...READ MORE

answered Apr 24, 2018 in Selenium by Vardy
• 2,360 points
197,673 views
0 votes
0 answers

How to handle browser popup in Selenium using Java?

Generally, we scrap the data from websites ...READ MORE

Jun 7, 2019 in Selenium by Vaishnavi
• 1,180 points
1,705 views
0 votes
1 answer

How to get the select options using Selenium WebDriver in Java

Hey there! You should be able to get ...READ MORE

answered Jun 14, 2019 in Selenium by Surya
• 970 points
4,730 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,568 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,084 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,178 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,840 views
0 votes
1 answer

How to handle Pop-up in Selenium WebDriver using Java

Actually, its pretty simple. Use this code ...READ MORE

answered Apr 6, 2018 in Selenium by nsv999
• 5,500 points
10,515 views
0 votes
2 answers

How to scroll the Page up or down in Selenium WebDriver? (using java)

JavascriptExecutor jsx = (JavascriptExecutor)driver; jsx.executeScript("window.scrollBy(0,555)", ""); or Action classes ...READ MORE

answered Sep 6, 2020 in Selenium by Sri
• 3,190 points
18,869 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP