How to handle drop downs using Selenium WebDriver in Java

0 votes

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
• 5,920 points
1,420 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,050 points

Related Questions In Selenium

+2 votes
1 answer

How can we handle authentication popup in Selenium WebDriver using Java

The Alert Method, authenticateUsing() lets us skip ...READ MORE

answered Apr 18, 2018 in Selenium by king_kenny
• 3,590 points
3,724 views
+9 votes
13 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,280 points
41,513 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 in Selenium by Vaishnavi
• 860 points
24 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 6 days ago in Selenium by Surya
• 860 points
26 views
0 votes
1 answer

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,050 points
145 views
0 votes
1 answer
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,130 points
537 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
135 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 5, 2018 in Selenium by nsv999
• 5,050 points
2,828 views
0 votes
1 answer

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

Its pretty simple. Check out the below ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,050 points
5,068 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.