无法找到检测页面上是否存在元素的方法

编程入门 行业动态 更新时间:2024-10-26 05:17:34
本文介绍了无法找到检测页面上是否存在元素的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我对 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

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

更多推荐

[db:关键词]

本文发布于:2023-04-26 21:14:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142801.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:是否存在   元素   页面   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!