坚持使用watir尝试创建一个循环来单击表中包含的所有链接。 目前该表格具有以下格式:
<table id="test"> <tbody><tr> <th>Firstname</th&t; <th>Lastname</th> <th>Link</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td><a href="http://facebook.com">http://facebook.com</a></td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td><a href="http://google.com">http://google.com</a></td> </tr> </tbody></table>我目前的尝试看起来像:
browser.table(:id => "test").rows do |tr| tr.each do |td| td.links.click end end上面的代码在浏览器中没有任何作用,也没有返回终端中的内容(没有错误,没有输出)。
还尝试了使用列的不同方法:
columns = browser.table(:id => "test").strings.transpose browser.columns.each do |t| t.click browser.back end这输出了这个错误:jsfiddle.rb:24:在<main>': undefined method列'用于#(NoMethodError)
Stuck with watir trying to create a loop to click in all the links included in a table. Currently the table has this format:
<table id="test"> <tbody><tr> <th>Firstname</th> <th>Lastname</th> <th>Link</th> </tr> <tr> <td>Jill</td> <td>Smith</td> <td><a href="http://facebook.com">http://facebook.com</a></td> </tr> <tr> <td>Eve</td> <td>Jackson</td> <td><a href="http://google.com">http://google.com</a></td> </tr> </tbody></table>And my current attempt looks like:
browser.table(:id => "test").rows do |tr| tr.each do |td| td.links.click end endThis code above does nothing in the browser & neither returns something in the terminal (no errors, no outputs).
Also tried a different approach using columns:
columns = browser.table(:id => "test").strings.transpose browser.columns.each do |t| t.click browser.back endThat outputs this error: jsfiddle.rb:24:in <main>': undefined methodcolumns' for # (NoMethodError)
最满意答案
这应该可以点击表格中的每个链接:
my_table = browser.table(:id, 'test') table_links = my_table.links.map(&:text) table_links.each do |link_text| my_table.link(:text, link_text).click browser.back end主要基于Justin Ko的答案。
This should work to click on each link in the table:
my_table = browser.table(:id, 'test') table_links = my_table.links.map(&:text) table_links.each do |link_text| my_table.link(:text, link_text).click browser.back endLargely based on Justin Ko's answer here.
更多推荐
发布评论