“为定位器找到了多个元素"警告 [英] "More than one element found for locator" warning

查看:28
本文介绍了“为定位器找到了多个元素"警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一项测试中,我使用 CSS 选择器 定位一个元素:

In one of my tests, I'm locating an element using a CSS selector:

element(by.css("ul.nav button"))

有多个元素与查询匹配,但是,因为我只需要第一个元素,所以我对选择器没问题.

There is more than one element matching the query, but, since I need only the first one, I'm okay with the selector.

问题是,protractor 抛出一个警告:

The problem is, protractor throws a warning:

警告 - 为定位器找到了多个元素By.cssSelector("ul.nav button") - 将使用第一个结果

WARNING - more than one element found for locator By.cssSelector("ul.nav button") - the first result will be used

是否可以抑制警告?换句话说,我怎样才能让 protractor 知道我知道这个问题并且不想再显示警告?

Is it possible to suppress the warning? In other words, how can I let protractor know that I'm aware of the problem and don't want the warning to be shown anymore?

使用 protractor 开发版本(直接从 master 分支安装).

Using protractor development version (installed directly from the master branch).

推荐答案

试试这个:

element.all(by.css("ul.nav button")).first()

基本上,这告诉量角器您已经知道有多个元素,而您只想要第一个(就像您在问题中所说的那样).

Basically, this tells Protractor that you already know there's more than one element, and you just want the first one (like you said in your question).

这篇关于“为定位器找到了多个元素"警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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