我有一个包含以下 html 的文本框。这id
是动态的,我无法使用它。我在文本框中填写了一些文本。<input type="text" id="117841" class="NEdit" title="MyText" maxlength="20" style="position: absolute; overflow: hidden; font-style: normal; font-weight: normal; font-family: arial; font-size: 12px; text-transform: uppercase; width: 184px; left: 28px; top: 0px; height: 14px;">
有一个添加按钮,当我单击时会创建一个新选项卡。在新选项卡中,我将有一个具有与上述相同属性的文本框,除了一个新的id
正在创建中。
<input type="text" id="118355" class="NEdit" title="MyText" maxlength="20" style="position: absolute; overflow: hidden; font-style: normal; font-weight: normal; font-family: arial; font-size: 12px; text-transform: uppercase; width: 184px; left: 28px; top: 0px; height: 14px;">
两个文本框都位于同一页面上,但位于不同的选项卡中,其中一个是visible
另一个不可见。选项卡显示为 div。选项卡和其中的字段之间没有关系。选项卡如下所示:
<div id="117285" class="NTabTabs" style="position: absolute; overflow: visible; left: 0px; top: 0px; width: 1211px; height: 20px;"><div class="NTabTab" id="117285t0" style="cursor: pointer; position: absolute; overflow: hidden; white-space: pre; text-align: center; left: 2px; top: 2px; width: 101px; height: 16px; padding-top: 1px; border-top-width: 1px; border-top-style: solid; border-left-width: 1px; border-left-style: solid; border-right-width: 1px; border-right-style: solid;">Tab#1</div><div class="NTabActiveTab" id="117285t1" style="cursor: pointer; position: absolute; overflow: visible; white-space: pre; text-align: center; left: 104px; top: 0px; width: 65px; height: 19px; padding-top: 1px; border-top-width: 1px; border-top-style: solid; border-left-width: 1px; border-left-style: solid; border-right-width: 1px; border-right-style: solid;">Tab#2</div></div>
如何根据这些文本框的可见性来识别它们?
谢谢你!
我认为最好的方法是找到两个元素之间的一些区别属性。例如,HTML 中至少必须有某些内容使其中一个可见,而另一个不可见。
但是,如果这不是一种选择,我认为剩下的唯一选择是迭代所有可能的文本字段并采用可见的文本字段。
例如,在以下页面中有两个文本字段,第一个不可见:
<html>
<body>
<div style="display:none;">
<input type="text" id="1">
</div>
<div>
<input type="text" id="2">
</div>
</body>
</html>
您可以使用文本字段访问器来遍历文本字段元素并选择第一个可见的元素。
class MyPage
include PageObject
text_field(:field){ text_field_elements.find(&:visible?) }
end
当使用页面对象时,你会看到field_element
将是第二个文本字段,因为它是可见的:
page = MyPage.new(browser)
p page.field_element.attribute('id')
#=> "2"
请注意,为了使示例保持简单,访问器的块将迭代所有文本字段。但是,您可以将其他参数传递给text_field_elements
更具体地了解要检查哪些文本字段。例如,以下访问器将查找具有特定标题的第一个可见文本字段:
text_field(:text2){ text_field_elements(:title => 'MyText').find(&:visible) }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)