【问题标题】:How to check whether Bottom of the page reached when I do scrolling滚动时如何检查页面底部是否到达
【发布时间】:2020-09-01 14:51:07
【问题描述】:

我必须截取整个页面的截图,但我无法成功(只有Firefox驱动程序支持截取完整截图)。所以我想将页面拆分为顶部,中心,底部并将其保存为三个屏幕截图,但这里的问题是,如果我编写以下代码

require 'watir'
b=Watir::Browser.new
b.scroll.to :top
b.screenshot.save("top.png")
b.scroll.to :center
b.screenshot.save("center.png")
b.scroll.to :bottom
b.screenshot.save("bottom.png")

不幸的是,无论页面是否需要滚动,上述代码都会截取三个屏幕截图。所以我的问题是,有什么方法可以知道 Page 是否已经到达底部?

【问题讨论】:

标签: ruby selenium watir


【解决方案1】:

您可以获取窗口高度以及正文内容高度,以估算您需要多少屏幕截图。我说“近似”,因为获取高度的方法有各种注意事项,例如处理滚动条。除非你真的需要像素完美的截图,否则这个近似值应该足够了。

# Determine how many screenshots are needed to get all of the content
window_height, content_height = b.execute_script('return [window.innerHeight, document.body.clientHeight];')
screenshot_pieces = content_height / window_height.to_f

# Always take a screenshot of the top
b.scroll.to :top
b.screenshot.save('top.png')

# If there are more than 2 pieces, take a screenshot of the center
if screenshot_pieces > 2 
  b.scroll.to :center
  b.screenshot.save('center.png')
end

# If there are more than 1 piece, take a screenshot of the bottom
if screenshot_pieces > 1
  b.scroll.to :bottom
  b.screenshot.save('bottom.png')
end

您可以根据自己的喜好调整上述条件。例如,如果您不希望屏幕截图有重叠部分(即不关心页面的小数部分),您可以将条件分别更改为screenshot_pieces >= 3screenshot_pieces >= 2

【讨论】:

  • 这真的解决了我的问题。非常感谢你hhhhhhhhhhh!
【解决方案2】:

您可以获取元素的位置,尝试向下滚动并查看元素位置是否已更改。如果没有,您可能处于“底部”,但有很多潜在的警告,这种情况将取决于上下文。

【讨论】:

  • 这对我不起作用,因为我设计了必须在页面末尾截取屏幕截图的框架。将使用该框架的人将留下这些屏幕截图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-30
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多