Watir-Webdriver-如果有多个具有相同属性的按钮,如何区分按钮

毫米波

该查询基于我之前的问题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>
贾斯汀·柯(Justin Ko)

如果要与之交互的元素上没有唯一属性,则必须查看其周围的元素。在这种情况下,h3元素文本区分了按钮。

解决方案-从唯一元素导航DOM

从概念上讲,最简单的解决方案是:

  1. 获取相关的h3元素。
  2. 导航到父div元素,它也是按钮的父元素。
  3. 获取该父div中的button元素。

可以使用以下方法完成此操作:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法使用watir-webdriver单击按钮

来自分类Dev

如何在watir和watir-webdriver中使用intellisense?

来自分类Dev

使用Watir Webdriver全屏

来自分类Dev

Watir-webdriver不断打开多个浏览器

来自分类Dev

Watir-webdriver不断打开多个浏览器

来自分类Dev

如何在ruby rspec(watir或watir-webdriver)脚本中多行注释?

来自分类Dev

如何使用watir-webdriver禁用Firefox中的下载窗口?

来自分类Dev

如何在watir Webdriver中单击表内的随机链接?

来自分类Dev

如何修改Watir-Webdriver使用的Firefox配置文件

来自分类Dev

如何使用watir-webdriver循环访问链接数组的href

来自分类Dev

Ruby Watir WebDriver Net :: ReadTimeout

来自分类Dev

Watir-Webdriver如何使用Watir浏览器在Chrome中获取嵌入式pdf文本

来自分类Dev

如何使用带有Selenium Watir-webdriver的Ruby执行Ctrl +多次鼠标单击操作?

来自分类Dev

如何使用带有Selenium Watir-webdriver的Ruby执行Ctrl +多次鼠标单击操作?

来自分类Dev

Selenium :: WebDriver :: Error :: ElementNotVisibleError而使用Watir循环单击按钮时

来自分类Dev

使用Ruby和watir-webdriver,如果知道文本,如何获取元素的XPath?

来自分类Dev

使用带有 Watir 和 Webdriver 的 Ruby 强制浏览器加载

来自分类Dev

如何使用Watir WebDriver选择在div标记中定义的组合列表文本项,其中所有div标记均具有相同的类名

来自分类Dev

使用watir-webdriver打开多个线程会导致“连接被拒绝”错误

来自分类Dev

如何使用watir-webdriver将脚本动态添加到页面

来自分类Dev

如何使用watir-webdriver遍历DOM(孩子/兄弟姐妹)?

来自分类Dev

如何设置MAC与Watir Webdriver和Ruby一起使用Selenium

来自分类Dev

如何选择下拉菜单,然后使用watir Webdriver单击下拉项

来自分类Dev

IE上的Watir Webdriver文本输入缓慢

来自分类Dev

使用Chrome Watir-WebDriver阻止图像

来自分类Dev

file_detector错误watir Webdriver

来自分类Dev

使用watir-webdriver选择本地文件

来自分类Dev

使用watir-webdriver保存图像

来自分类Dev

使用watir-webdriver指定'optgroup'

Related 相关文章

  1. 1

    无法使用watir-webdriver单击按钮

  2. 2

    如何在watir和watir-webdriver中使用intellisense?

  3. 3

    使用Watir Webdriver全屏

  4. 4

    Watir-webdriver不断打开多个浏览器

  5. 5

    Watir-webdriver不断打开多个浏览器

  6. 6

    如何在ruby rspec(watir或watir-webdriver)脚本中多行注释?

  7. 7

    如何使用watir-webdriver禁用Firefox中的下载窗口?

  8. 8

    如何在watir Webdriver中单击表内的随机链接?

  9. 9

    如何修改Watir-Webdriver使用的Firefox配置文件

  10. 10

    如何使用watir-webdriver循环访问链接数组的href

  11. 11

    Ruby Watir WebDriver Net :: ReadTimeout

  12. 12

    Watir-Webdriver如何使用Watir浏览器在Chrome中获取嵌入式pdf文本

  13. 13

    如何使用带有Selenium Watir-webdriver的Ruby执行Ctrl +多次鼠标单击操作?

  14. 14

    如何使用带有Selenium Watir-webdriver的Ruby执行Ctrl +多次鼠标单击操作?

  15. 15

    Selenium :: WebDriver :: Error :: ElementNotVisibleError而使用Watir循环单击按钮时

  16. 16

    使用Ruby和watir-webdriver,如果知道文本,如何获取元素的XPath?

  17. 17

    使用带有 Watir 和 Webdriver 的 Ruby 强制浏览器加载

  18. 18

    如何使用Watir WebDriver选择在div标记中定义的组合列表文本项,其中所有div标记均具有相同的类名

  19. 19

    使用watir-webdriver打开多个线程会导致“连接被拒绝”错误

  20. 20

    如何使用watir-webdriver将脚本动态添加到页面

  21. 21

    如何使用watir-webdriver遍历DOM(孩子/兄弟姐妹)?

  22. 22

    如何设置MAC与Watir Webdriver和Ruby一起使用Selenium

  23. 23

    如何选择下拉菜单,然后使用watir Webdriver单击下拉项

  24. 24

    IE上的Watir Webdriver文本输入缓慢

  25. 25

    使用Chrome Watir-WebDriver阻止图像

  26. 26

    file_detector错误watir Webdriver

  27. 27

    使用watir-webdriver选择本地文件

  28. 28

    使用watir-webdriver保存图像

  29. 29

    使用watir-webdriver指定'optgroup'

热门标签

归档