我有一个简单的定义列表,我试图将信息解析出来并放入我稍后可以在我的代码中使用的变量中。我想从此表中获取产品版本,序列号,系统时间和正常运行时间。
<table class="tabbed_table" cellspacing="5">
<tbody>
<tr>
<td>
<dl class="table-display">
<dt class="wide">Product version: </dt>
<dd class="wide">1.0</dd>
<dt class="wide">Serial number:</dt>
<dd class="wide">D00005</dd>
<dt class="wide">System Time:</dt>
<dd class="wide">Tuesday, October 07, 2014 04:04PM CDT</dd>
<dt class="wide">Uptime:</dt>
<dd class="wide"> 16:04:17 up 1 day, 3:32, 1 user, load average: 0.00, 0.04, 0.00 </dd>
</dl>
</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
DList类(在Watir-WebDriver中)和Dl类(在Watir-Classic中)有一个to_hash
方法,可以从dl元素中创建一个哈希值。键将是dt元素文本,值将是dd元素文本。
# Convert the dl element into a hash:
descriptions = browser.dl(class: 'table-display').to_hash
# Output the values (or any other usage):
p descriptions['Product version:']
#=> "1.0"
p descriptions['Serial number:']
#=> "D00005"
p descriptions['System Time:']
#=> "Tuesday, October 07, 2014 04:04PM CDT"
p descriptions['Uptime:']
#=> "16:04:17 up 1 day, 3:32, 1 user, load average: 0.00, 0.04, 0.00"
答案 1 :(得分:0)
您可以在这里使用的最精确的选择器是xPath:
a = Watir::Browser.new :chrome
a.goto 'http://THEPAGEURLGOESHERE.COM'
descriptions = a.dts(xpath: "//table[@class='tabbed_table']//dl[@class='table-display']/dt[@class='wide']").map { |element| element.text }
values = a.dds(xpath: "//table[@class='tabbed_table']//dl[@class='table-display']/dd[@class='wide']").map { |element| element.text }
p descriptions.zip(values) #=>
# [["Product version:", "1.0"], ["Serial number:", "D00005"], ["System Time:", "Tuesday, October 07, 2014 04:04PM CDT"], ["Uptime:", "16:04:17 up 1 day, 3:32, 1 user, load average: 0.00, 0.04, 0.00"]]
a.close