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

问题描述 投票:0回答:1

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

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]

怎么做?

javascript html testing css-selectors webdriver-io
1个回答
0
投票

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

就像是:

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

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

Here's a working example

© www.soinside.com 2019 - 2024. All rights reserved.