Watir 元素存在但不存在,但在屏幕截图中可见

Watir Element exists but not present, but is visible in screenshot

出于某种原因,Watir 说我的页面页脚不可见。

我的测试设置可以截图,我可以在页面截图上看到页脚。

我通过以下方式截取屏幕截图:

@browser.driver.save_screenshot(output + "/" + screenshot)

代码中的测试行:

puts "waiting for bottom sticker"
Watir::Wait.until { @browser.div(:id => "bottom-sticker").exists? }
puts "bottom sticker found"
puts "bottom sticker exists: " + @browser.div(:id => "bottom-sticker").exists?.to_s
puts "bottom sticker present: " + @browser.div(:id => "bottom-sticker").present?.to_s
puts "bottom sticker visible: " + @browser.div(:id => "bottom-sticker").visible?.to_s

测试日志:

  waiting for bottom sticker
  bottom sticker found
  bottom sticker exists: true
  bottom sticker present: false
  bottom sticker visible: false

Watir 报告页面中的其他元素可见且存在。底部贴纸 div 是 body 的子项,因此它没有被父项隐藏。

有什么想法吗?

您需要等到元素出现。

现在您正在等待它存在,这意味着它只是在等待在 html 中找到该元素,而不是在屏幕上。 Waiting until present 将等到 HTML AND 中的元素出现在屏幕上。请参考以下注释以了解要使用的断言:

Watir 中使用了三种不同的方法来确定对象在浏览器中的状态。它们是 .exists?、.present? 和 .visible?。

首选方法是.present?

三者的区别是:

.存在?

  • returns 如果元素在 HTML 中(无论是否显示在 屏幕与否)
  • returns false 如果元素不在 HTML

.礼物?

  • returns 如果元素在 HTML 中并显示在 屏幕
  • returns false 如果元素在 HTML 中并且不显示在 屏幕或如果元素不在 HTML

.可见?

  • returns 如果元素在 HTML 中并显示在 屏幕
  • returns false 如果元素在 HTML 中并且不显示在 屏幕
  • 如果元素不在 HTML
  • 中则抛出错误

我发现这个问题不是因为 watir,而是源于 selenium。我最终找到了这个 link 到 Selenium docs。我的 div 上确实有 overflow:hidden 样式,删除它后内容在 Watir 中可用。

Selenium 一定有问题,因为内容肯定没有溢出,而且在使用 selenium 浏览器截取的屏幕截图中肯定是可见的。