该查询基于我之前的问题Watir-Webdriver-无法找到具有ID,名称,文本,值的按钮,我获得了一些有关如何通过元素获取字段的详细信息。但是现在在我的屏幕之一中,我有两个具有相同class和div的按钮。所以我不能拿我需要的那个。我需要获取具有“支票和储蓄”产品的按钮。有人可以帮忙获得确切的按钮单击吗?我的Web应用程序是在AnjularJS上开发的。
这是确定按钮的页面中HTML的完整div部分。
<div class="jl-layout">
<div class="jl-layout-50-50">
<!-- ngRepeat: entry in ctrl.vm.productSuites.entries -->
<div style="" ng-repeat="entry in ctrl.vm.productSuites.entries" class="item product-container centered ng-scope">
<h3 class="ng-binding">Checking and Savings</h3>
<button type="button" tabindex="0" ng-click="ctrl.submitProductSelection(entry)" class="extra-button-padding secondary jl-button">
<ng-transclude>
<span class="ng-scope">Select</span>
</ng-transclude>
</button>
<div tabindex="0" role="button" ng-click="ctrl.submitProductSelection(entry)" class="mobile-click"/>
</div>
<!-- end ngRepeat: entry in ctrl.vm.productSuites.entries -->
<div style="" ng-repeat="entry in ctrl.vm.productSuites.entries" class="item product-container centered ng-scope">
<h3 class="ng-binding">Savings</h3>
<button type="button" tabindex="0" ng-click="ctrl.submitProductSelection(entry)" class="extra-button-padding secondary jl-button">
<ng-transclude>
<span class="ng-scope">Select</span>
</ng-transclude>
</button>
<div tabindex="0" role="button" ng-click="ctrl.submitProductSelection(entry)" class="mobile-click"/>
</div>
<!-- end ngRepeat: entry in ctrl.vm.productSuites.entries -->
</div>
</div>
如果要与之交互的元素上没有唯一属性,则必须查看其周围的元素。在这种情况下,h3元素文本区分了按钮。
解决方案-从唯一元素导航DOM
从概念上讲,最简单的解决方案是:
可以使用以下方法完成此操作:
product = 'Checking and Savings'
browser.h3(text: product).parent.button.click
解决方案-遍历相关元素组
另一种方法是遍历代表一个项目的每个div。对于每个div(项目),您将检查它是否是正确的产品,然后单击按钮。
这将是:
browser.divs(class: 'item').find { |div| div.h3(text: product).exists? }.button.click
请注意,根据您的特定HTML代码段,此代码将始终返回第一个按钮。缺少结束div标签的原因<div tabindex="0" role="button" ng-click="ctrl.submitProductSelection(entry)" class="mobile-click"/>
是使所有内容都显示在同一项目div中。
解决方案-使用XPath
这与第一个解决方案相同。但是,这是使用单个XPath定位器完成的。好处是由于较少的方法调用而速度更快。但是,它可能更难阅读。
browser.button(xpath: "//h3[text()='#{product}']/../button").click
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句