如何在Watir的HTML中获取具有相同属性的元素数量? [英] How to get the number of elements having same attribute in HTML in Watir?

查看:38
本文介绍了如何在Watir的HTML中获取具有相同属性的元素数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HTML 文档,其中包含具有相同类名的元素.我可以对页面中的所有元素进行迭代,并在列表中存储具有类名的元素.Watir 有没有更好的方法来计算所有具有相同类名的 HTML 元素?这个问题XML 中具有相同属性的元素数量解决了这个问题,但我有两个与此相关的查询

I have a HTML document which contains elements having the same class name. I could just do an iteration over all the elements in a page and store with elements with a class name in a list. Is there a better way in Watir to get a count of all the HTML elements having the same class name? This question Count of Elements with same attribute in XML kind of addresses the issue, but I had two queries related to that

  1. 如果 HTML 文档不是严格的 XHTML 文档怎么办?
  2. 如果不同类型的 HTML 元素具有相同的类会怎样?

示例 HTML 文件可以是:

Sample HTML files could be:

相同类型的元素具有相同的类名

Elements of the same type having same class name

<input type="password" class="foo" /> 
<input type="text" class="foo" />

具有相同类名的不同类型元素

Elements of different types having same class name

<input type="password" class="foo" /> 
<span class="foo"></span>
<a href='1' class="foo">Text</a>

提前谢谢各位

推荐答案

如果您正在使用 watir-webdriver 宝石:

If you are using watir-webdriver gem:

1)

HTML

<input type="password" class="foo" /> 
<input type="text" class="foo" />

browser.elements(:class => "foo").size
# => 2 

2)

HTML

<input type="password" class="foo" /> 
<span class="foo"></span>
<a href='1' class="foo">Text</a>

browser.elements(:class => "foo").size
# => 3 

这篇关于如何在Watir的HTML中获取具有相同属性的元素数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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