How to get option value of select element

后端 未结 6 1046
故里飘歌
故里飘歌 2021-01-03 23:40

I am trying to get the option value of a select element using Protractor. However, I\'m not able to find the option element.

HTML

&l         


        
6条回答
  •  伪装坚强ぢ
    2021-01-04 00:08

    I have had some problems getting dropdowns working well, and have spent some time today working it out (and therefore I'm sharing it here in case someone else finds it useful).

    On earlier versions of Protractor there was a by.selectedOption, which effectively does the same thing as by.select, but returns only the selected item. So, to get the text of the selected option above, you could have:

    expect(element(by.selectedOption('model')).getText()).toEqual('Option 1');
    

    I wrote a blog post if you want more detail, it also covers a helper function for selecting the option in the dropdown: http://technpol.wordpress.com/2013/12/01/protractor-and-dropdowns-validation/

    Recent versions of protractor have removed this function, replacing it with:

    expect(element(by.id('my_id')).element(by.css('option:checked')).getText();
    

    Which is more flexible, as it can be applied to any of the finders, whereas selectedOption only worked with a model finder.

提交回复
热议问题