量角器获得了出口的第二个元素

发布于 2025-02-03 14:30:08 字数 703 浏览 4 评论 0原文

我有一个问题,我对量显影剂很新。

我需要通过文本获得一个元素,现在不可能更改UI构建以使用更好的选择器的方式。

带有文本摘要的标签

const myElement = element(by.xpath("//span[text()='Summary']"));

因此,我现在拥有的是span 但是,屏幕上只有一个元素,但是,当有两个元素时,它会失败,但出现以下错误:

  • 失败:元素不相互作用

可能是因为它正在尝试访问切换的第一个元素。 因此,我尝试在阅读时访问元素索引:

element(by.xpath("//span[text()='Summary']")[1]);

并收到以下错误:

失败:无效的定位器

我在某个地方读到值以1始于1而不是0的地方,因此我尝试了:

element(by.xpath("//span[text()='Summary']")[2]);

但是,我仍然遇到相同的错误:

失败:无效的定位器

可以找到这些第二个元素吗?

I have an issue and I'm quite new with protractor.

I need to get an element by its text, right now it is not possible to change how the UI is built to use better selectors.

So what I have right now is a span tag with the text Summary inside of it, to retrieve the element using XPath I do the following:

const myElement = element(by.xpath("//span[text()='Summary']"));

That works for the first occurrence when there's only one element on the screen, however, when there are two elements, it fails with the following error:

  • Failed: element not interactable

Possibly because it is trying to access the first element which is toggled.
So I tried accessing the element index as I read around with:

element(by.xpath("//span[text()='Summary']")[1]);

And got the following error:

Failed: Invalid locator

I read somewhere that the values start on 1 and not on 0 so I tried with:

element(by.xpath("//span[text()='Summary']")[2]);

Yet, I'm still getting the same error:

Failed: Invalid locator

Is there a way I can get to these second element?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2025-02-10 14:30:08

当您使用列表切片时,就有更多的元素。因此,您可以尝试使用元素而不是元素

element(by.xpath("(//span[text()='Summary'])[2]");

element(by.xpath("(//span[contains(text(),'Summary')])[2]");

When you are using list slicing then there exist more then one elements. So you can try to use elements instead of element

element(by.xpath("(//span[text()='Summary'])[2]");

OR

element(by.xpath("(//span[contains(text(),'Summary')])[2]");
書生途 2025-02-10 14:30:08

您可以尝试此XPATH,它有其他括号,您几乎已经到了。

(// span [text()='summary'])[2]

参考:
xpath查询以获取元素的nth实例

You can try this XPath, it has additional brackets, you have been almost there.

(//span[text()='Summary'])[2]

Reference:
XPath query to get nth instance of an element

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文