【发布时间】:2016-12-21 20:23:22
【问题描述】:
我正在使用具有以下 HTML 的网页,我想使用页面对象内的 text_field 来识别 <span> 内的第一个 <input> 字段。
<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>
如果我使用:id => '132:' 来识别该字段,则一切正常。 IE。 text_field(:target_user_name, :id => '132:' ) 有效。
问题是这个 HTML 是由底层应用程序 (SAP) 生成的,它并不总是为 <input> 字段 id 生成相同的值,因此不能依赖使用 id 来一致地识别元素.
因此,鉴于上面的 HTML,我还有什么其他方法可以可靠地找到这个 <input> 字段。
我尝试了以下方法,但均无效。他们中的大多数都超时等待元素被定位。
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')
有什么想法吗?
【问题讨论】:
-
作为用户,您将如何识别文本字段?可能有一些用户可见的文本让用户知道该文本字段的用途。我会使用该文本及其与该字段的关系来进行识别。
-
有一个 div,其中包含围绕输入的跨度周围的一些字段标签。
-
关于上面的评论 - 在这种情况下,XPATH 看起来会是什么样子会找到输入字段?本来以为我输入字段属性的 XPATHS 会起作用。
-
这将取决于 HTML 到底是什么。 XPath 非常依赖于 HTML 的结构,因此如果您可以扩展您的 HTML sn-p 以包含该外部 div,将会有所帮助。
-
刚刚更新了 sn-p 以显示外部 div。
标签: xpath watir watir-webdriver page-object-gem