5

私はwatirテストが初めてです。誰かが次の要素を見つけるのを手伝ってくれませんか?

<div class="location_picker_type_level" data-loc-type="1">
  <table></table>
</div>

div私はこれを見つけるのが好きdata-loc-typeですtable

元:

browser.elements(:xpath,"//div[@class='location_picker_type_section[data-loc-type='1']' table ]").exists?
4

1 に答える 1

13

Watir はdata-型属性をロケーターとして使用することをサポートしています (つまり、xpath を使用する必要はありません)。ダッシュをアンダースコアに置き換え、先頭にコロンを追加するだけです。

以下を使用して div を取得できます (属性のロケーターの形式に注意してください: data-loc-type -> :data_loc_type ):

browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')

このタイプの div が 1 つしかないことが予想される場合は、次のようにしてテーブルがあることを確認できます。

div = browser.div(:class => 'location_picker_type_level', :data_loc_type => '1')
puts div.table.exists?
#=> true

一致する複数の div があり、それらの少なくとも 1 つにテーブルがあることを確認したい場合は、コレクションのany?メソッドを使用します。divs

#Get a collection of all div tags matching the criteria
divs = browser.divs(:class => 'location_picker_type_level', :data_loc_type => '1')

#Check if the table exists in any of the divs
puts divs.any?{ |d| d.table.exists? }
#=> true

#Or if you want to get the div that contains the table, use 'find'
div = divs.find{ |d| d.table.exists? }
于 2013-03-22T14:24:47.300 に答える