0

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

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>
4

1 回答 1

0

一种方法是从表中收集单元格,创建一个散列来存储数据,然后使用该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
于 2013-10-30T17:46:50.533 回答