Selenium或Watir Webdriver,如何保存调整大小的屏幕截图? [英] Selenium or Watir Webdriver, how to save resized screenshot?

查看:612
本文介绍了Selenium或Watir Webdriver,如何保存调整大小的屏幕截图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试获取大小调整后的Google 400x400屏幕截图.我在Selenium和Watir中都尝试过,但没有成功.

require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'google.com'
b.window.resize_to(400,400)
b.driver.save_screenshot("screenshot.jpg")

我总是以原始浏览器大小获得屏幕截图.

任何想法如何将其调整为400x400大小?

解决方案

当前,WebDriver将屏幕快照定义为整页屏幕截图".也就是说,整个DOM应该由save_screenshot方法生成的图像表示. Chrome驱动程序不会生成完整DOM的屏幕快照的事实是 Chrome驱动程序中的错误.因此,真正的答案是无法使用WebDriver生成仅浏览器视图端口的屏幕​​截图.

话虽如此,根据您的操作系统,可能还可以使用其他编程方式来实现此目的.例如,在Windows上,很容易获得桌面窗口的窗口句柄(HWND),捕获桌面图像(使用Windows GetDesktopPrintWindow API)并使用由提供的坐标来裁剪它. WebDriver Window API .

I try to get a resized 400x400 screenshot of google. I try this both in Selenium and Watir with no success.

require 'watir-webdriver'
b = Watir::Browser.new
b.goto 'google.com'
b.window.resize_to(400,400)
b.driver.save_screenshot("screenshot.jpg")

I always get the screenshot with the original browser size.

Any idea how can I save it resized to 400x400?

解决方案

At present, WebDriver defines a screenshot as a "full page screenshot". That is, the entire DOM should be represented by the image generated by the save_screenshot method. The fact that the Chrome driver doesn't generate a screenshot of the full DOM is a bug in the Chrome driver. So the real answer is that there is no way to generate a screenshot of only the browser view port using WebDriver.

Having said that, it might be possible to use other programmatic means to accomplish this, depending on your OS. On Windows, for example, it would be pretty easy to get the desktop window's window handle (HWND), capture the image of the desktop (using the Windows GetDesktop and PrintWindow APIs), and cropping it using the coordinates supplied by the WebDriver Window API.

这篇关于Selenium或Watir Webdriver,如何保存调整大小的屏幕截图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆