webdriverio循环浏览选择元素中的选项 [英] webdriverio Loop through options in select element
问题描述
HTML示例:
<select id="random_text">
<option value="option1">asadka_TEST</option>
<option value="option2">Peter Parker</option>
<option value="option3">Clark Kent</option>
<option value="option4">aldkfsd_TEST</option>
</select>
类中的Javascript代码
Javascript code in class
class TestPage extends Page {
get fullNameSelect() {return browser.element('#random_text');}
iterateAndSelect() {
this.fullNameSelect.value.ForEach() //pseudo code
}
}
我希望 iterateAndSelect 函数迭代所有选项,然后选择以"_ TEST"结尾的第一个选项.
I want iterateAndSelect function to iterate through all options and select first option that ends with "_TEST".
到目前为止,我仅弄清楚有selectByVisibleText
操作,但是问题是我想根据值以"_TEST"字符串和这个动作我必须提供确切的值.
So far, I've only figured out that there is selectByVisibleText
action but the problem is that I want to select option based on condition that value ends with "_TEST" string, and with this action I have to provide the exact value.
推荐答案
最简单的方法是找到以_TEST结尾的第一个选项的索引:
The simplest way would be to find the index of the first option that ends with _TEST:
const index = browser.getElements('option').findIndex(option => {
return option.getText().endsWith('_TEST')
});
然后您可以使用该索引选择该值:
Then you can select that value by using that index:
browser.getElement('select').selectByIndex(index);
这篇关于webdriverio循环浏览选择元素中的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!