webdriverio循环浏览选择元素中的选项 [英] webdriverio Loop through options in select element

查看:246
本文介绍了webdriverio循环浏览选择元素中的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆