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

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

问题描述

在我的一项测试中,我正在使用 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.

问题是,量角器发出警告:

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

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

是否可以禁止显示警告?换句话说,如何让量角器知道我已经知道了问题并且不希望再显示警告?

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?

使用量角器开发版本(直接从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天全站免登陆