小编Jay*_*ark的帖子

似乎不能使用 SitePrism 的 element.visible?如果元素不存在

我有以下代码:

element :header_upgrade_subscription, :xpath, "//a[text()='Upgrade subscription']"
element :header_change_subscription, :xpath, "//a[text()='Change subscription']"

if header_upgrade_subscription.visible?
  change_subscription = header_upgrade_subscription 
else
  change_subscription = header_change_subscription 
end
Run Code Online (Sandbox Code Playgroud)

问题是如果 header_upgrade_subscription 不存在,它只会失败:

Capybara::ElementNotFound Exception: Unable to find xpath "//a[text()='Upgrade subscription']"
Run Code Online (Sandbox Code Playgroud)

我知道在水豚,你可以这样做:

(rdb:1) first(:xpath, "//a[text()='Upgrade subscription']")
nil
Run Code Online (Sandbox Code Playgroud)

如果它不存在,它将返回 nil。我将如何对 SitePrism 元素使用“第一个”方法?这就是我得到的:

(rdb:1) first(header_upgrade_subscription)
Capybara::ElementNotFound Exception: Unable to find xpath "//a[text()='Upgrade subscription']"
Run Code Online (Sandbox Code Playgroud)

我喜欢使用“first”方法,因为如果元素不存在,它没有等待时间。

谢谢你的帮助!

capybara site-prism

2
推荐指数
1
解决办法
2242
查看次数

标签 统计

capybara ×1

site-prism ×1