2017-12-01 15 views
1

selectboxの現在選択されているオプション値を取得するにはどうすればよいですか?分度器で選択したオプション値を取得する方法

以下のコードを試しましたが、機能しません。

element(by.css('#selectbox')).all(by.tagName('option')).then(function (options) { 
    options.forEach(option => { 
     if (option.getAttribute('selected')) { 
      console.log('selected option', option); 
     } 
    }); 
}); 

助けてください。分度器でselectとの対話/対処するための全体的なよりよい方法があること

element(by.css('#selectbox option[selected]')) 

注:

+1

[方法の可能性の重複選択要素のオプション値を取得する](https://stackoverflow.com/questions/18401812/how-to-get-option-value-of-select-element) –

答えて

0

は "選択" とされた属性またはクラスVALU e?

それはコードでは

「を選択された要素は、その値を読み出します」として動作します:

//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と、さらに重要here about CSS-Selectors

0

あなたはどちらか選択したオプションをフィルタリング、またはCSSセレクタを直接ターゲットに.filter()を使用することができます。

関連する問題