【问题标题】:How to get the index of the element pointed to by the selector in WebdriverIOWebdriverIO中如何获取选择器指向的元素的索引
【发布时间】:2018-09-11 10:08:27
【问题描述】:

我在 Chrome 中使用 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]

怎么做?

【问题讨论】:

  • 你的意思是它相对于父级的索引吗?如 :nth-child()?从零开始还是从一开始?
  • 我想要最近的父母的索引。 > 从零开始或从一开始都可以。

标签: javascript html testing css-selectors webdriver-io


【解决方案1】:

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

类似:

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

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

Here's a working example.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    相关资源
    最近更新 更多