如何使用Watir Webdriver获取父元素的索引? [英] How to get index of parent element using Watir Webdriver?

查看:52
本文介绍了如何使用Watir Webdriver获取父元素的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个HTMLcode:

I have this HTMLcode:

<div class="A">
  <div class="B">
    Text1
  </div>
</div>
<div class="A">
  <div class="B">
    Text2
  </div>
</div>

所以我需要在找到一些文本的地方找到div class ='A'的索引.我使用Watir Webdriver,现在有了以下代码:

So i need to find index of div class='A' where i find some text. I use Watir Webdriver and now i have this code:

if @ff.div(:class=>'A').div(:text=>'Text1')
  then ind=@ff.div(:class=>'A').index
end

但是这当然不能说'未定义的局部变量或方法'index'.

but of course this doesn't work saying 'undefined local variable or method `index''.

推荐答案

无法使用WebDriver获取索引,因为它们与匹配元素的集合有关.您可以尝试将元素文本收集为Array,然后获取目标文本的索引.

There's no way to get the index using the webdriver since they are relative to the collection of matched elements. What you can try to do is collect the elemetns text as Array and then get the index of the target text.

@@ff.divs(:class, 'A').collect(&:text).index('Text1')

请注意,这仅适用于简单的场景(例如您的示例).如果您需要处理更复杂的案件,请更新示例以符合实际情况.

Note that this will only work for simple scenarios (like your example). If you need to attack a more complex case update your example to match the real scenario.

这篇关于如何使用Watir Webdriver获取父元素的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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