我有以下代码:
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
问题是如果 header_upgrade_subscription 不存在,它就会失败:
Capybara::ElementNotFound Exception: Unable to find xpath "//a[text()='Upgrade subscription']"
我知道在 capybara 中,您可以:
(rdb:1) first(:xpath, "//a[text()='Upgrade subscription']")
nil
如果不存在则返回nil。我将如何对 SitePrism 元素使用“第一”方法?这是我得到的:
(rdb:1) first(header_upgrade_subscription)
Capybara::ElementNotFound Exception: Unable to find xpath "//a[text()='Upgrade subscription']"
我喜欢使用“first”方法,因为如果元素不存在,它没有等待时间。
感谢您的帮助!
最佳答案
if has_header_upgrade_subscription? && header_upgrade_subscription.visible?
关于capybara - 似乎无法使用 SitePrism 的 element.visible?如果元素不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21294549/