开发者

finding the direct child of a tag in watir

I have a table containing multiple columns in which the data is populated from a database. The columns can have drop-downs, text-fields, check-boxes along with simple text. I need to write down a function which will essentially return the data present in a table column.

Here's an example as to how the tags are named in the web page. [Credits to w3schools for the CSS for the tables].

<html>
<head>
<style type="text/css">
#customers
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
width:100%;
border-collapse:collapse;
}
#customers td, #customers th 
{
font-size:1em;
border:1px solid #98bf21;
padding:3px 7px 2px 7px;
}
#customers th 
{
font-size:1.1em;
text-align:left;
开发者_运维百科padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#ffffff;
}
#customers tr.alt td 
{
color:#000000;
background-color:#EAF2D3;
}
</style>
</head>

<body>
<table id="customers">
<tr>
  <th>Company</th>
  <th>Contact</th>
  <th>Country</th>
</tr>
<tr class="data-row">
<td id="customers:0:company">Alfreds Futterkiste</td>
<td id="customers:0:contact">Maria Anders</td>
<td id="customers:0:chooseCountry">
<select id="customers:0:country">
<option>Germany</option>
<option>Sweden</option>
<option>Mexico</option>
</select>
</td>
</tr>
<tr class="data-row alt">
<td id="customers:1:company">Berglunds snabbköp</td>
<td id="customers:1:contact">Christina Berglund</td>
<td id="customers:1:chooseCountry">
<select id="customers:1:country">
<option>Germany</option>
<option selected="selected">Sweden</option>
<option>Mexico</option>
</select>
</td>
</tr>
<tr class="data-row">
<td id="customers:2:company">Centro comercial Moctezuma</td>
<td id="customers:2:contact">Francisco Chang</td>
<td id="customers:2:chooseCountry">
<select id="customers:2:country">
<option>Germany</option>
<option>Sweden</option>
<option selected="selected">Mexico</option>
</select>
</td>
</tr>
</table>
</body>
</html>

Now, the algorithm which I use for determining all the values in the column say "Company" is

  1. Determine the no of rows having class = or substring as "data-row".
  2. Construct the string for getting the cell and iterating it from 0 to n-1
  3. Use the text method to retrieve the text

Now, if I use this on a select_list, it returns all the options in the list. Thus, I was going to check whether the child of the tag is either a text-field or a drop-down list and call their respective functions to get their values.

Is there a way where in Watir we can determine the child of a particular tag is a particular tag or not or is there some method similar to getAllChildNodes in JavaScript?

Sorry for being over-descriptive and thanks in advance for any possible solutions.


This is very simple, you just need to see whether a text_field or select_list exists:

require 'watir-webdriver'

b = Watir::Browser.start 'yourwebpage'

b.table.rows.each do |row|
  row.cells.each do |cell|
    if cell.text_field.exist?
      puts cell.text_field.value
    elsif cell.select_list.exist?
      puts cell.select_list.selected_options
    else
      puts cell.text
    end
  end
end

b.close
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜