1

データベースからデータが入力される複数の列を含むテーブルがあります。列には、ドロップダウン、テキスト フィールド、チェック ボックス、および単純なテキストを含めることができます。テーブル列に存在するデータを本質的に返す関数を書き留める必要があります。

以下は、Web ページでのタグの命名方法の例です。[テーブルの CSS は w3schools に感謝]。

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

さて、「会社」という列のすべての値を決定するために使用するアルゴリズムは

  1. class = または部分文字列を「data-row」として持つ行の数を決定します。
  2. セルを取得して 0 から n-1 まで繰り返すための文字列を作成します
  3. テキストメソッドを使用してテキストを取得します

これを select_list で使用すると、リスト内のすべてのオプションが返されます。したがって、タグの子がテキスト フィールドかドロップダウン リストかを確認し、それぞれの関数を呼び出して値を取得します。

Watir で特定のタグの子が特定のタグであるかどうかを判断できる方法はありますか、または JavaScript の getAllChildNodes に似た方法はありますか?

説明が多すぎて申し訳ありません。可能な解決策を事前に感謝します。

4

1 に答える 1

4

これは非常に簡単です。text_field または select_list が存在するかどうかを確認するだけです。

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
于 2011-08-30T11:03:05.160 に答える