【问题标题】:Ruby Watir selecting text from tableRuby Watir 从表格中选择文本
【发布时间】:2013-10-31 03:04:48
【问题描述】:

我有一个选项卡式表,我试图从“活动”列中获取正在运行的各种服务的值。

Services
    Service                    Active
    SERVICE1                   YES
    SERVICE2                   NO
    SERVICE3                   YES

我希望从桌子上使用这样的东西。但这似乎对我不起作用。目标是选择其中一项服务并确定它是否处于活动状态 YES 或 NO 并将其放入变量中。你们在以前的回答中给了我很大的帮助,我非常感谢您的帮助和投入。

browser.td(:text => "SERVICE1").parent.td(:index => 1).flash 

当我尝试使用上面的代码时遇到这样的错误

/home/bill/.rvm/gems/ruby-1.9.2-p320@vts_automated/gems/watir-webdriver-0.6.4/lib/watir-webdriver/elements/element.rb:490:in `assert_exists': unable to locate element, using {:id=>"services", :tag_name=>"td"} (Watir::Exception::UnknownObjectException)

我的html代码是这样的

<table class="tabbed_table" cellspacing="5">
<tbody>
<tr>
<td>
<h2>Appliance</h2>
<dl class="table-display">
<dt class="wide">Product version: </dt>
<dd class="wide">xxxxx</dd>
<dt class="wide">Serial number:</dt>
<dd class="wide">xxxxxx</dd>
<dt class="wide">System Time:</dt>
<dd class="wide">Wednesday, October 30, 2013 02:02PM CDT</dd>
</dl>
</td>
<td>
<h2>Services</h2>
<table id="services">
<tbody>
<tr>
<th>Service</th>
<th>Active</th>
</tr>
<tr class="alt">
<td class="no_bg">SERVICE1</td>
<td class="no_bg">YES</td>
</tr>
<tr class="normal">
<td class="no_bg"> SERVICE2 </td>
<td class="no_bg">NO</td>
</tr>
<tr class="alt">
<td class="no_bg"> SERVICE3 </td>
<td class="no_bg">YES</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>

【问题讨论】:

  • 你能仔细检查你的 watir 代码和异常吗?您的 watir 代码显示您正在寻找 td(:text =&gt; "SERVICE1") 但异常表明您正在寻找其他东西 - {:id=&gt;"services", :tag_name=&gt;"td"}
  • 是的,我会忽略我列出的 watir 代码行。那是我尝试过但没有奏效的东西。 td(:text => "SERVICE1")。我用从表格方法收集单元格尝试了你的建议。即使表名是“服务”。定位此元素时遇到问题。所以现在看来​​,cells = browser.table(:id => "services").tds 行有问题。我只是不确定为什么它找不到这个元素。我的 watir 代码是在下面剪切并粘贴您的建议。

标签: ruby watir


【解决方案1】:

一种方法是从表中获取collect the cells,创建一个散列来存储数据,然后使用each_slice 方法将数据分配给散列。此时,您应该拥有所需的数据,并且可以根据需要对其进行操作。例如:

# get cells from table(:id => "services") 

cells = browser.table(:id => "services").tds

# create hash

service_active = {}

# iterate over cells variable using each_slice method and assign keys/values to has

cells.each_slice(2) do |slice|
  service_active["#{slice[0]}"] = slice[1]
end

# hash with data

service_active.each {|k,v| puts "the value of #{k} is #{v}"}

#=> the value of Service is Active
#=> the value of SERVICE1 is YES
#=> the value of SERVICE2 is YES
#=> the value of SERVICE3 is YES

【讨论】:

  • 谢谢,我试试看
  • 与我之前得到的类似错误。由于某种原因,它似乎无法找到表“服务”:“无法找到元素,使用 {:id=>"services", :tag_name=>"table"}"
  • 此页面上的表格不止一个,所以可能我没有剪切和粘贴足够的 html 代码。我已经更新了上面描述中的 html 代码,希望它更清楚。我很抱歉第一次没有捕捉到所有这些。
  • 抱歉没有帮助。查看 Justin Ko 的博客文章,了解如何解决“未知元素”错误:jkotests.wordpress.com/2013/02/06/…。这可能会有所帮助...
  • 当然。添加您的解决方案作为答案,接受它,应该就是这样。
猜你喜欢
  • 2015-07-27
  • 2016-06-29
  • 2012-06-26
  • 2018-06-17
  • 2012-09-06
  • 1970-01-01
  • 1970-01-01
  • 2015-02-17
  • 1970-01-01
相关资源
最近更新 更多