ruby - finding specific element which exists in multiple places but has same id,class -
how find element if exists in multiple places in page has same id under same class? example: there 2 text fields same id , choose 2nd one. works when write watir/ruby(without using page object) @b.text_fields(:id => 'color').last.set "red"
but unsuccessful far make work using page object.
thanks in advance
as mentioned in comments, best solution update fields have unique ids.
however, assuming not possible, can solve problem using :index
locator. following page object finds 2nd color field, equivalent watir's @b.text_field(:id => 'color', :index => 1).set
:
class mypage include pageobject text_field(:color_1, id: 'color', index: 0) text_field(:color_2, id: 'color', index: 1) end
which called like:
page = mypage.new(browser) page.color_1 = 'red' page.color_2 = 'blue'
if trying last field, ie replicate @b.text_fields(:id => 'color').last.set
, :index
"-1":
text_field(:color_2, id: 'color', index: -1)
note similar can done when locating fields dynamically within method (as opposed defined accessor):
class mypage include pageobject def set_colors(color_1, color_2) text_field_element(id: 'color', index: 0).value = color_1 text_field_element(id: 'color', index: 1).value = color_2 end end