本文介绍了无法找到检测页面上是否存在元素的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Watir 很陌生.

I'm very new to Watir.

我有一些 Ruby/Watir 代码,用于检测元素是否存在,如果存在,请单击它,如果不存在,请单击其他元素.这两个元素,每次都出现.不幸的是,我尝试过的一切都不起作用.

I have a bit of Ruby/Watir code that is supposed to detect if an element, exists, and if so, click it, if not, click a different element. Both elements, show up, every time. Unfortunately nothing I've tried works.

if browser.contains_text("/media/images/icons/reviewertools/editreview.jpg")
then browser.image(:src => "/media/images/icons/reviewertools/editreview.jpg").click
else browser.image(:src => "/media/images/icons/reviewertools/savereview.jpg").click
end

这最终失败了无法定位元素,使用 {:src=>"/media/images/icons/reviewertools/savereview.jpg"} (Watir::Exception::UnknownObjectException)"

This eventually fails with "Unable to locate element, using {:src=>"/media/images/icons/reviewertools/savereview.jpg"} (Watir::Exception::UnknownObjectException)"

它应该点击了/editreview.jpg,这是可见的.

It should have clicked /editreview.jpg, which was visible.

我也试过:

if browser.image("/media/images/icons/reviewertools/savereview.jpg").exists
then browser.image(:src => "/media/images/icons/reviewertools/savereview.jpg").click

以及:

if browser.image("/media/images/icons/reviewertools/savereview.jpg").exists?

请注意,这些情况都没有检测到元素,或者没有这样做,请执行 else 子句.

Note that NONE of these cases detect the element, or failing to do that, execute the else clause.

如果您做出回应,请提供具体的代码示例以供您提出建议.

Please, if you respond, provide specific code examples for your suggestions.

推荐答案

您需要对单击和存在使用相同的语法吗?方法.Kat、Chuck 和 Kinofrost 也指出了这一点.

You need to use the same syntax with both the click and the exists? methods. This has also been pointed out by Kat and Chuck and Kinofrost.

if browser.image(:src => "/media/images/icons/reviewertools/editreview.jpg").exists?
then browser.image(:src => "/media/images/icons/reviewertools/editreview.jpg").click
else browser.image(:src => "/media/images/icons/reviewertools/savereview.jpg").click
end

这篇关于无法找到检测页面上是否存在元素的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:39