简体   繁体   English

使用watir-webdriver单击'div'中的所有链接

[英]Click all links inside 'div' with watir-webdriver

I have the following DOM setup and I'm trying to click each/every link using watir-webdriver. 我有以下DOM设置,我试图使用watir-webdriver单击每个/每个链接。 Keep in mind that both 'Random Number X' and 'Random Name X' are random and can not be used to come up with the solution. 请记住,“随机数X”和“随机名称X”都是随机的,不能用于提出解决方案。

<div class="container">
<ul>
<li id="Random Number 1"><a href="#">Random Name 1</a></li>
<li id="Random Number 2"><a href="#">Random Name 2</a></li>
<li id="Random Number 3"><a href="#">Random Name 3</a></li>
</ul>
</div>

Something like: 就像是:

browser.div(:class=>"container").links.each do | link | 
  link.click 
  browser.back
end

You have to store all the links in an Array or structure, and then you will be able to click on all the links of a web page or a div or any element. 您必须将所有链接存储在数组或结构中,然后您才能单击网页或div或任何元素的所有链接。

link = Array.new
i = 0
browser.div(:class, "container").links.each do |li|
link[i] = l.text
i = i + 1
end

li.each do |visit|
b.link(:text, visit).click
b.back
end

This is required because if you are not storing the links in to array then with the simple loop will click on first link only and when it will execute browser.back, it will not get the value of second link to click as every time cache will be cleared. 这是必需的,因为如果您没有将链接存储到数组中,那么使用简单循环将仅单击第一个链接,并且当它将执行browser.back时,它将不会获得第二个链接的值,因为每次缓存都将被清除。

You could also try: 你也可以尝试:

browser.div(:class, 'container').as.each do |x|
    x.click
    browser.back
end

or to hit an individual link try one of the following: 或点击单个链接尝试以下之一:

browser.div(:class, 'containter').as[0].click  #This is for the first link.   
browser.div(:class, 'containter').a(:text, 'Random Name 1').click 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM