Watir Webdriver 计算 UL 列表中的项目数量
我进行了一些搜索,但无法找到合适的答案。基本上我有一个长度不同的无序列表。我想遍历列表,做一些其他事情,然后返回并选择列表中的下一个项目。当我定义循环应该迭代的次数时,我可以很好地做到这一点,因为我知道列表中的项目数量。
但是我不想为每个测试定义这个,我想获取列表中的项目数,然后将其弹出到一个变量中,我可以使用该变量退出循环并执行我想要的下一步操作。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您想要收集的信息以及您打算将其用于什么目的,以下是通常完成的方式。您可以让它在到达最后一个元素时自然停止,而不是获取一个数字来定义迭代,然后迭代该次数:
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: