循环在与Watir的表中(Loop in table with Watir)

编程入门 行业动态 更新时间:2024-10-11 11:17:22
循环在与Watir的表中(Loop in table with Watir)

坚持使用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 end

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

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

Largely based on Justin Ko's answer here.

更多推荐

本文发布于:2023-07-28 03:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1299878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:在与   Watir   table   Loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!