How to get selected option value in protractor

后端 未结 2 1232
长发绾君心
长发绾君心 2021-01-16 23:37

How can I get currently selected option value of selectbox?

I have tried below code but it\'s not working.

element(by.css(\'#selectbox\')).all(by.tag         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-17 00:22

    Is "selected" and attribute or a class-value?

    It would work as "get element, which is selected, then read out its value"

    In code:

    //if selected is an attribute
    element(by.css('#selectbox option[selected]')).getAttribute('value').then(function(value){
        console.log('selected option is '+value);
    })
    
    //if selected is a class-attribute
    element(by.css('#selectbox option.selected')).getAttribute('value').then(function(value){
        console.log('selected option is '+value);
    })
    
    //note, that "element(by.css())" === "$()", so this is the same as the first one
    $('#selectbox option[selected]').getAttribute('value').then(function(value){
        console.log('selected option is '+value);
    })
    

    Read more here about locators and even more important here about CSS-Selectors

提交回复
热议问题