Watir-webdriver:使用索引访问元素

时间:2022-02-12 19:14:10

I am trying to access a li element using indexing

我正在尝试使用索引访问li元素

<div class="item-list">
<ul>
<li class="views-row views-row-1 views-row-odd views-row-first">
<li class="views-row views-row-2 views-row-even">
<li class="views-row views-row-3 views-row-odd">
<li class="views-row views-row-4 views-row-even">
<li class="views-row views-row-5 views-row-odd">
<li class="views-row views-row-6 views-row-even">
<li class="views-row views-row-7 views-row-odd">
<li class="views-row views-row-8 views-row-even">
<li class="views-row views-row-9 views-row-odd views-row-last">
</ul>
</div>

The code I am using is

我正在使用的代码是

@browser.div(:class,'item-list').ul.li(:index => 2)

The question is : These are elements on a page and I will be using a loop to access each element. I thought using indexing will solve the problem but when I write my code and execute it I get the following error

问题是:这些是页面上的元素,我将使用循环访问每个元素。我认为使用索引可以解决这个问题,但是当我编写代码并执行它时,我得到了下面的错误

expected #<Watir::LI:0x2c555f80 located=false selector={:index=>2, :tag_name=>"li"}> to exist (RSpec::Expectations::ExpectationNotMetError)

How can I access these elements using Indexing.

如何使用索引访问这些元素。

2 个解决方案

#1


4  

If you've got class-naming that nice, forget indexing! Do a partial match on the "views-row" parameter:

如果你的类命名很好,那就不要索引了!对“views-row”参数进行部分匹配:

@browser.li(:class => /views-row-1/)

This can easily be parameterized for looping (although I don't know what you're doing with the information so this loop will not be very exciting).

这可以很容易地参数化为循环(尽管我不知道您正在对信息做什么,因此这个循环不会非常令人兴奋)。

x = 0
until x==9
  x+=1
  puts  @browser.li(:class => /views-row-#{x}/).text
end

You could also blindly loop through the li's contained in your div if you'd like:

如果你愿意的话,你也可以盲目地循环浏览你的部门所包含的li:

   @browser.div(:class,'item-list').lis.each do |li|
      puts li.text
   end

#2


2  

According to the Watir wiki, Watir supports the :index method on the li element. So unless it is a bug in watir-webdriver, I think the index should work.

根据Watir wiki, Watir支持li元素上的:index方法。所以,除非它是watir-webdriver中的一个bug,否则我认为这个索引应该是有用的。

You may want to try the watir mailing list to see if this is a problem for others.

您可能想尝试一下watir邮件列表,看看这是否对其他人来说是一个问题。

#1


4  

If you've got class-naming that nice, forget indexing! Do a partial match on the "views-row" parameter:

如果你的类命名很好,那就不要索引了!对“views-row”参数进行部分匹配:

@browser.li(:class => /views-row-1/)

This can easily be parameterized for looping (although I don't know what you're doing with the information so this loop will not be very exciting).

这可以很容易地参数化为循环(尽管我不知道您正在对信息做什么,因此这个循环不会非常令人兴奋)。

x = 0
until x==9
  x+=1
  puts  @browser.li(:class => /views-row-#{x}/).text
end

You could also blindly loop through the li's contained in your div if you'd like:

如果你愿意的话,你也可以盲目地循环浏览你的部门所包含的li:

   @browser.div(:class,'item-list').lis.each do |li|
      puts li.text
   end

#2


2  

According to the Watir wiki, Watir supports the :index method on the li element. So unless it is a bug in watir-webdriver, I think the index should work.

根据Watir wiki, Watir支持li元素上的:index方法。所以,除非它是watir-webdriver中的一个bug,否则我认为这个索引应该是有用的。

You may want to try the watir mailing list to see if this is a problem for others.

您可能想尝试一下watir邮件列表,看看这是否对其他人来说是一个问题。