【问题标题】:Watir : recognizing image buttonWatir:识别图像按钮
【发布时间】:2012-04-25 13:43:40
【问题描述】:

我正在尝试使用 Watir 完成重复性任务,但遇到了困难,需要您的帮助。我有下面的按钮代码:

<button class="ppm_button " onclick="submitForm('page','more=1');" type="button">
  <img border="0" align="absmiddle" title="Add and Select More" alt="Add and Select More" style="width: 11px; height: 8px; background: url("ui/uitk/images/nde.png") no-repeat scroll 0px 0px transparent;" src="ui/uitk/images/s.gif" >
  &nbsp;Add and Select More
</button>

我尝试使用 firebug 获取 xpath 并使用相同的 xpath 在 ff 上运行脚本,但它失败了。我很难给出适当的 xpath 来识别对象。

任何帮助将不胜感激。

【问题讨论】:

  • 请提供您的 xpath。并尝试类似//img[@title='Add and Select More']
  • 除非绝对必要,否则我建议不要使用 xpath。
  • 是的,避免使用 xpath,我从来没有将它与 watir 一起使用。没有它,代码读起来会更好。如果以下两个答案都不适合您,那么这件事是否会在框架内?

标签: xpath watir watir-webdriver


【解决方案1】:

您应该能够避免使用 xpath。

尝试使用 Watir 选择器,例如:

b.img(:src => "ui/uitk/images/s.gif")

然后您应该能够将您想要对元素执行的任何操作附加到末尾,例如.click

这超出了https://github.com/watir/watir/wiki/HTML-Elements-Supported-by-Watir 的可用属性,但我不确定您使用的是什么版本的 Watir,或者该列表的最新情况。

根据它你也可以使用:

b.img(:alt => "Add and Select More")

【讨论】:

    【解决方案2】:

    除非没有其他方法,否则我会避免使用 xpath,与其他识别所需元素的方法相比,它往往会生成丑陋且难以阅读的代码。

    我认为@anonygoose 应该可以工作,但如果没有其他尝试的方法是单击按钮本身,可以按如下方式完成(注意我假设你想单击按钮,否则替换 .根据需要使用其他方法单击)

    browser.button(:class => 'ppm_button ').click
    

    browser.button(:text => /Add and Select More/).click
    

    或者如果出于某种奇怪的原因,这些都不起作用,您可以使用其中之一

    browser.img(:src => "ui/uitk/images/s.gif").parent.click
    
    browser.img(:alt => "Add and Select More").parent.click
    

    如果这些都不适合您,请编辑您的问题并包括您尝试过的问题以及您得到的信息。

    【讨论】:

      【解决方案3】:

      这个应该可以!

      browser.img(:src =&gt; "url of the base image").parent.click。只需提及缩略图使用的图像的 url(在检查元素中)。

      【讨论】:

        猜你喜欢
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-13
        • 2021-12-22
        相关资源
        最近更新 更多