I'm working with a web page with the following HTML where I want to identify the first <input>
field inside the <span>
using a text_field from within a page-object.
<div id="131:"> Please enter your name:
<span class="autocompspan " style="position:static;">
<input style="position: static;" class="autocompinput yui-ac-input" id="132:" name="132:"
onfocus="juic.fire("132:","_focus",event);"
onchange="juic.fire("132:","_despatchChange",event);"
onblur="juic.fire("132:","_blur",event);" size="60"
onkeydown="juic.fire("132:","_onkeydown",event);"
onkeyup="juic.fire("132:","_onkeyup",event);" aria-disabled="false" value=""
role="combobox" aria-autocomplete="list" aria-owns="132:_divList"
aria-activedescendant="132:_divAD" findtype="proxy" delimchar="" hideusername="false"
fetchusername="false" autocomplete="off" type="text">
<input value="" id="132:_hidden" name="132:_hidden" type="hidden">
</span>
</div>
If I use :id => '132:'
to identify the field things work fine. I.e. text_field(:target_user_name, :id => '132:' )
works.
The issue is that this HTML is generated by the underlying app (SAP) which does not always generated the same value for the <input>
field id
so using the id
cannot be relied upon to consistently identify the element.
So, given the above HTML what other ways might I go about reliably finding this <input>
field.
I've tried the following, none of which work. Most of them time out waiting for the element to be located.
text_field(:target_user_name, :xpath => "//*[@class='autocompinput yui-ac-input' and @role = 'combobox']" )
text_field(:target_user_name, :xpath => "//*[@class='autocompinput' and @role = 'combobox']" )
text_field(:target_user_name, :xpath => "//span/input[@class='autocompinput yui-ac-input' and @role = 'combobox']" )
text_field(:target_user_name, :xpath => "//input[@class='autocompinput yui-ac-input' and @role = 'combobox']" )
text_field(:target_user_name, :class => 'autocompinput yui-ac-input')
Any thoughts?
When an element does not have unique identifiable attributes, you should look at the elements around it. In this case, there is user visible text that helps a user identify the purpose of the field. That same text can be used to identify the element in Watir.
As the surrounding
div
only contains the labelling text, you can search for thatdiv
by its text and get the only text field in it:As a page-object accessor: