量角器条件选择器 [英] Protractor Conditional Selector

查看:93
本文介绍了量角器条件选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium Protractor,希望从以下列表中选择所有元素,但其中一个包含文本"Cat",然后对其余元素执行一些操作.

I am using Selenium Protractor and want to select all elements from the following list except one that contains text "Cat" and then perform some operations on the remaining ones.

<div class="mainDiv">
    <div class="childDiv">Dog</div>
    <div class="childDiv">Goat</div>
    <div class="childDiv">Bird</div>
    <div class="childDiv">Cat</div>
    <div class="childDiv">Zebra</div>
</div>

是否有cssContainingText(或其他一些选择器)提供的选择器,我可以在其中提供条件以选择除包含文本"Cat"的元素以外的所有元素?

Is there a selector by cssContainingText (or some other) in which I can give a condition to select all elements except the one containing text "Cat"?

推荐答案

您可以使用以下定位器策略:

  • xpath :

//div[@class='mainDiv']//div[@class='childDiv'][not(contains(.,'Cat'))]

使用

:contains伪类不在CSS Spec中,并且Firefox或Chrome(甚至在WebDriver外部)也不支持.您可以在 selenium.common.exceptions.InvalidSelectorException中找到详细的讨论,其中"span:contains('string')"

但是,如果元素始终以特定顺序(例如,以 DOM树)出现在 DOM树中. 总是在第四个孩子处,您也可以使用:

However, if the elements always appears within the DOM Tree in a specific order, e.g. Cat always at the forth child, you can also use:

  • cssSelector :

div.mainDiv div.childDiv:not(:nth-child(4))

您可以在以下位置找到一些相关的讨论:

You can find a couple of relevant discussions in:

  • While fetching all links,Ignore logout link from the loop and continue navigation in selenium java
  • How to write a CSS Selector selecting elements NOT having a certain attribute?

这篇关于量角器条件选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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