如何在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
- 如果 HTML 文档不是严格的 XHTML 文档怎么办?
- 如果不同类型的 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