WebdriverIO中如何获取选择器指向的元素的索引

How to get the index of the element pointed to by the selector in WebdriverIO

我使用 WebdriverIO 和 Chrome 编写测试。 我正在尝试指定一个包含特定文本的选择器并获取匹配元素的索引。

HTML:

...
<div class="parent">
    <span>lorem</span>
    <span>ipsum</span>
    <span>dolor</span>
    <span>sit</span>
    <span>amet</span>
</div>
...

测试:

let result = browser.ANYTHING_WDIO_API('.parent span*=dolor');
// -> `result` expected: 3 or [3]

怎么做?

您可以尝试类似于 this answer 的解决方案。

类似于:

const elements = $$('.parent span')

const index = elements.findIndex(function (el) {
    return el.getText() === 'dolor';
});

Here's a working example.