Protractor Conditional Selector

前端 未结 1 559
悲哀的现实
悲哀的现实 2021-01-15 04:35

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 remainin

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-15 05:28

    You can create a List selecting all the elements except one that contains text Cat using the following Locator Strategy:

    • xpath:

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

    When using Selenium and css-selectors:

    The :contains pseudo-class isn't in the CSS Spec and is not supported by either Firefox or Chrome (even outside WebDriver). You can find a detailed discussion in selenium.common.exceptions.InvalidSelectorException with “span:contains('string')”

    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))
      

    Reference

    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?

    0 讨论(0)
提交回复
热议问题