Watir Webdriver 计算 UL 列表中的项目数量

发布于 2024-12-01 21:31:15 字数 1262 浏览 1 评论 0原文

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

但是我不想为每个测试定义这个,我想获取列表中的项目数,然后将其弹出到一个变量中,我可以使用该变量退出循环并执行我想要的下一步操作。

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 我可以使用以下内容:

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

但不能使用 webdriver。

我想我也许可以使用“lis”,但我只得到一个十六进制结果:

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

谢谢,

保罗

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.

The HTML is like so:

<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>

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

But not with webdriver.

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

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

Thanks,

Paul

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的快感 2024-12-08 21:31:15

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

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 #

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 #
凉栀 2024-12-08 21:31:15
puts browser.ul(:id => "PageContent_cat").lis.length
puts browser.ul(:id => "PageContent_cat").lis.length
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文