首页 > 解决方案 > 带有 ruby​​ 和 watir 中的 URL 的整页屏幕截图

问题描述

使用下面的代码,我只截取了屏幕的一部分,但我想截取完整的截图,其中还显示了页面的 URL。

可能吗?

AfterStep do
  encoded_img = @browser.driver.screenshot_as(:base64)
    embed("data:image/png;base64,#{encoded_img}",'image/png')
end

标签: rubyselenium-webdriverautomated-testswatir

解决方案


Watir 没有此规定来捕获屏幕截图和 URL。但是我们可以使用 win32ole 来做到这一点。

require 'win32/screenshot'

Win32::Screenshot::Take.of(:desktop).write(image_path)

在我使用 URL 捕获全屏截图的情况下,我执行以下操作

# Code to capture the full-page screenshot with a URL
require 'watir'
require 'win32ole'
require 'win32/screenshot'

# Launch a browser and navigate to the page
browser = Watir::Browser.new :ie
browser.goto "https://www.google.com"

win_title = browser.title #Fetch the Title

# Use AutoIt to get the focus of the browser to front
WIN32OLE.new("AutoItX3.Control").ControlFocus(win_title, "", "")

# Capture the screen shot of the desktop
sleep 2 # Hold for 2s
image_path = "image_path#{rand(10000)}.png"
Win32::Screenshot::Take.of(:desktop).write(image_path)

推荐阅读