protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Proper Way of Selecting an Option from a Dropdown List

Open demisx opened this issue 12 years ago • 16 comments

Hi. What's the current best way of doing this in protractor tests:

select('orderProp').option('Alphabetical')

for

<select ng-model="orderProp">
   <option value="name">Alphabetical</option>
   <option value="age">Newest</option>
 </select>

This was taken from the tutorial on http://docs.angularjs.org/tutorial/step_04 (last test at the end of the page). I am trying to convert e2e tests to protractor as an exercise and can't figure out a clean way to select an option from a drop down list. Some examples found on the web seemed too verbose and, perhaps, outdated. Thank you.

demisx avatar Mar 10 '14 20:03 demisx

I usually do something like:

var select = element.one(by.model('orderProp'));
select.$('[value="age"]').click();

khepin avatar Mar 11 '14 03:03 khepin

Sorry, not following the element.one(...) call. What is method "one"? Maybe:

select = element(by.model('orderProp'));`?

demisx avatar Mar 11 '14 07:03 demisx

YES! this is absolutely what I meant! Sorry for the confusion.

var select = element(by.model('orderProp'));
select.$('[value="age"]').click();

khepin avatar Mar 11 '14 07:03 khepin

No worries! I thought there was some secret method I wasn't aware of. :) Thank you for the input. I'll give it a try in am.

demisx avatar Mar 11 '14 07:03 demisx

Worked great! Thank you so much.

Just a recap for others. Until protractor implements a more natural way of selecting an option from a dropdown list, use @khepin method below. This is the cleanest one I've found so far.

Given this dropdown:

<select ng-model="orderProp">
   <option value="name">Alphabetical</option>
   <option value="age">Newest</option>
 </select>

And I want to select "Newest" option in my spec, do this:

// JavaScript
element(by.model('orderProp')).$('[value="age"]').click();

// CoffeeScript
element(By.model('orderProp')).$('[value="name"]').click()

demisx avatar Mar 11 '14 16:03 demisx

Hi there, the solution you have suggested above is not working for me and my dropdown element is not having model attribute. it has id locator and under that i have some options.

SeleniumShooters avatar Feb 02 '16 09:02 SeleniumShooters

Hi,

I tried the way @demisx proposed, but wasn't working fine completely. I modified what he recommends dividing it in two steps as:

element(by.model('orderProp')).click(); $('[value="age"]').click();

Im not shure if the first step is necessary... Try this @SeleniumShooters, this is working for me 10/10.

aldorianmode avatar Jun 14 '16 15:06 aldorianmode

I read this solution from somewhere and it is perfect that selecting by option number, and therefore wrote a method that takes an element and the optionNumber, and selects that optionNumber. If the optionNumber is null it selects nothing (leaving the dropdown unselected).

var selectDropdownbyNum = function ( element, optionNum ) {
  if (optionNum){
    var options = element.findElements(by.tagName('option'))   
      .then(function(options){
        options[optionNum].click();
      });
  }
};

adnanghaffar07 avatar Dec 01 '16 23:12 adnanghaffar07

Hi, My scenario: I wanted to search address and need to click the search suggestions. I have a html like below:

Address Results
Hyde, Cheshire England United Kingdom
HydeSt, Spotswood VIC 3015

Below is my protractor spec: element.all(by.css('.tt-suggestion.tt-selectable')).first().click();

I can able to select and click the search suggestions on chrome, InternetExplorer, MicrosoftEdge. But in firefox, click() is not happening.

Could you please anyone help me.

krishnaGottipati avatar Dec 09 '16 12:12 krishnaGottipati

guys use this command

select obj=new select(driver.findElement(by.xpath("//select[@class='gwt-ListBox']"))); obj.selectbyvisibleText(" "); guys as per your convenience use locators and depends on situation use selectbyvalue or selectbyindex

vishwabharatraju avatar May 29 '17 05:05 vishwabharatraju

It is really working thanku @demisx // JavaScript element(by.model('orderProp')).$('[value="age"]').click();

dhanrajpru avatar Mar 24 '18 07:03 dhanrajpru

Can anyone tell me that for jasemine language in protractor, this will be the code of selecting one option out of dropdown list. element.findElements(by.tagName('option')) ,will this be used inplace of "....element.all()..."???? var selectDropdownbyNum = function(element, optionNum) { if (optionNum) { var options = element.all(by.tagName('option')) .then(function(options) { options[optionNum].click(); });

anjurawatkathait avatar May 28 '18 10:05 anjurawatkathait

@123-anju it didn't worked for me. i used element.all(...)...

Davinci-Leonardo avatar Jul 11 '18 17:07 Davinci-Leonardo

Thank you using my code

On Wed, Jul 11, 2018, 10:36 PM Asargiotakis [email protected] wrote:

@123-anju https://github.com/123-anju it didn't worked for me. i used element.all(...)...

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/angular/protractor/issues/600#issuecomment-404242674, or mute the thread https://github.com/notifications/unsubscribe-auth/AbsBJwa_aFF-g2MSn-wu0I7CfwrlYE2Nks5uFjB9gaJpZM4BoZ_m .

vishwabharatraju avatar Jul 11 '18 17:07 vishwabharatraju

YES! this is absolutely what I meant! Sorry for the confusion.

var select = element(by.model('orderProp'));
select.$('[value="age"]').click();

This is perfect!! works for me /:

thirupathi-d avatar Sep 02 '20 06:09 thirupathi-d

Hi I want to display all the value coming in Dropdown using Typescript protractor can anyone tell me how to do it

softskul avatar Feb 11 '21 10:02 softskul