在 Ruby Watir 中使用 XPath 进行健全性检查

发布于 2024-07-22 09:21:56 字数 1045 浏览 2 评论 0原文

我正在使用 Ruby Watir 库为客户端进行自动化测试,但 XPath 选择器出现问题。 我想我只是需要另一双眼睛让我知道我是否错过了什么。

这是我正在使用的选择器:

puts ie.cell(:xpath, "//img[@src='3.jpg']/../").text

对于这组表,它按预期工作并打印“第三张图像”:

<table>
  <tr>
    <td><img src="1.jpg">First Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="2.jpg">Second Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="3.jpg">Third Image</td>
  </tr>
</table>

但是当我删除第二个表时,它正在破坏:

<table>
  <tr>
    <td><img src="1.jpg">First Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="3.jpg">Third Image</td>
  </tr>
</table>

使用上面的 put 代码,我在第二个示例中收到此错误:

Watir::Exception::UnknownObjectException: Unable to locate element, using :xpath, "//img[@src='3.jpg']/../"

I'm using the Ruby Watir library to do automated testing for a client and I'm having issues with the XPath selector. I think I just need another set of eyes to let me know if I'm just missing something.

Here is the selector I'm using:

puts ie.cell(:xpath, "//img[@src='3.jpg']/../").text

For this set of tables, it works as expected and prints "Third Image":

<table>
  <tr>
    <td><img src="1.jpg">First Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="2.jpg">Second Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="3.jpg">Third Image</td>
  </tr>
</table>

But is is breaking when I remove the second table:

<table>
  <tr>
    <td><img src="1.jpg">First Image</td>
  </tr>
</table>
<table>
  <tr>
    <td><img src="3.jpg">Third Image</td>
  </tr>
</table>

Using the puts code above I get this error on the second example:

Watir::Exception::UnknownObjectException: Unable to locate element, using :xpath, "//img[@src='3.jpg']/../"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-07-29 09:21:57

我重现了这个问题,并重新启动浏览器(IE6)为我修复了它。

I reproduced the problem, and restarting the browser (IE6) fixed it for me.

唱一曲作罢 2024-07-29 09:21:57

对于当前版本的 Watir,更好的方法是

browser.img(:src => '3.jpg').parent.text

For current versions of Watir the better way to do this would be

browser.img(:src => '3.jpg').parent.text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文