查找没有特定类名的元素 [英] Locating elements without a specific class name

查看:109
本文介绍了查找没有特定类名的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与我之前提出的问题相同- Watir Webdriver;计算具有变化的类名的元素.我能够使用类名称asset-card selectable添加特定数量的元素.

This is in the same vein as this question I asked before - Watir webdriver; counting elements with changing class names. I was able to add the specific number of elements based on using the class name asset-card selectable.

当前,我希望单击更多元素以将它们添加到现有集中.这是我的困境:

Currently, I am looking to click on more elements to add them to the existing set. Here is my dilemma:

  • 未单击元素 时,类名称为:
    image-card asset-card selectable
  • 单击元素时,将selected附加到类名:
    image-card asset-card selectable selected
  • When an element is not clicked, the class name is:
    image-card asset-card selectable
  • When an element is clicked, selected is appended to the class name:
    image-card asset-card selectable selected

就我而言,我正在尝试寻找仅 image-card asset-card selectable并且不包括selected的其他元素.我不确定如何明确找到它们.

In my case, I am trying to look for additional elements that only say image-card asset-card selectable and do not include selected. I am not sure how to explicitly locate them.

是否有正则表达式解决方案或有关如何实现此目的的其他想法?

Is there a regex solution or any other thoughts on how to approach this?

推荐答案

查找没有特定类的元素的最简单方法是使用:css定位符.

The easiest way to find an element without a certain class is to use a :css locator.

browser.divs(css: 'div.image-card.asset-card.selectable:not(.selected)')

CSS选择器的说明:

An explanation of the CSS-selectors:

  • div-这是您要匹配的元素的标签名称
  • .image-card.asset-card.selectable-这是您希望匹配元素具有的类的列表
  • :not(.selected)-这是您不希望拥有匹配元素的类的列表
  • div - This is the tag name of the elements you are trying to match
  • .image-card.asset-card.selectable - This is the list of classes you want the matching elements to have
  • :not(.selected) - This is the list of classes you do not want the matching elements to have

如果您反对CSS选择器,则可以遍历元素.请注意,这将变慢,因为它必须对浏览器进行更多调用.

If you are opposed to CSS-selectors, you could iterate over the elements. Note that this will be slower as it has to make more calls to the browser.

selectable = browser.divs(class: 'image-card asset-card selectable')
unselected = selectable.reject { |e| e.class_name.split(' ').include?('selected') }

我之前曾尝试使用正则表达式(请参见

I have tried to use a Regular Expression before (see my blog post), however it gets pretty messy. I would not recommended going that way.

这篇关于查找没有特定类名的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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