Watir Webdriver 计算 UL 列表中的项目数量 [英] Watir Webdriver counting number of items in a UL list

查看:15
本文介绍了Watir Webdriver 计算 UL 列表中的项目数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了几次搜索,但找不到合适的答案.基本上我有一个无序列表,它可以是不同的长度.我想遍历列表,做一些其他的事情,然后回来选择列表中的下一个项目.当我定义循环应该迭代的次数时,我可以做到这一点,因为我知道列表中的项目数量.

I've done a few searches and I'm unable to find a suitable answer. Basically I have an unordered list which can be of a varying length. I want to iterate through the list, do some other things and then come back and select the next item on the list. I can do this fine when I define the amount of times my loop should iterate as I know the amount of items in the list.

然而,我不想为每个测试定义这个,我想获取列表中的项目数,然后将其弹出到一个变量中,我可以用它来退出循环并做我想做的下一件事.

However I don't want to define this for each test, I want to grab the number of items in the list and then pop that into a variable that I can use to exit the loop and do the next thing I want.

HTML 是这样的:

<ul id="PageContent_cat">
  <li class="sel">
    <a target="_self" href="/searchlocation.aspx?c=S1">S1</a>
  </li>
  <li>
    <a target="_self" href="/searchlocation.aspx?c=S2">S2</a>
  </li>
  <li>
    <a target="_self" href="/searchlocation.aspx?c=S3">S3</a>
  </li>
  <li>
    <a target="_self" href="/searchlocation.aspx?c=S4">S4</a>
  </li>
  <li>
    <a target="_self" href="/searchlocation.aspx?c=S5">S5</a>
  </li>
  <li>
    <a target="_self" href="/searchlocation.aspx?c=S6">S6</a>
  </li>
  <li>
    <a target="_self" href="/searchlocation.aspx?c=S7">S7</a>
  </li>
</ul>

所以我可以看到列表中有 7 个项目.显然在 watir 我可以使用以下内容:

So I can see there are 7 items in the list. Apparently in watir I could have used something the following:

arr= ie.select_list(:name,'lr').getAllContents.to_a

arr= ie.select_list(:name,'lr').getAllContents.to_a

但不适用于网络驱动程序.

But not with webdriver.

我以为我可以使用lis",但我只得到了一个十六进制结果:

I thought I could maybe use 'lis' but I just get a Hex result:

$bob = browser.ul(:id => "PageContent_cat").lis把 $bob

$bob = browser.ul(:id => "PageContent_cat").lis puts $bob

谢谢,

保罗

推荐答案

根据您想要收集的信息以及您打算将其用于什么目的,以下是通常完成的方式.与其获取一个数字来定义您的迭代,然后再迭代该次数,您可以让它在到达最后一个元素时自然停止:

Depending on the information you're wanting to gather and what purpose you're going to put it to, here is the way that is typically done. Rather than getting a number to define your iterations and THEN iterating that number of times, you can have it stop naturally when it reaches the last element:

MyList = browser.ul(:id => "PageContent_cat")

#Scrape links from the UL for visiting
MyList.links.each do |link|
  puts link
  puts link.text
  b.goto(link)
  #etc
end

#Save li items to an array for later processing
MyArray = []

MyList.lis.each do |li|
  puts li.text
  MyArray << li.text
  #etc
end

#Iterate through your array in the same method, to report/visit/etc
MyArray.each do |item|
  puts "I collected something: #{item}"
  b.goto(item)
end #

这篇关于Watir Webdriver 计算 UL 列表中的项目数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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