简体   繁体   English

Selenium或Watir Webdriver,如何保存调整大小的截图?

[英]Selenium or Watir Webdriver, how to save resized screenshot?

I try to get a resized 400x400 screenshot of google. 我试图获得一个重新调整大小的谷歌400x400截图。 I try this both in Selenium and Watir with no success. 我在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")

I always get the screenshot with the original browser size. 我总是使用原始浏览器大小获取屏幕截图。

Any idea how can I save it resized to 400x400? 任何想法如何保存它调整为400x400?

At present, WebDriver defines a screenshot as a "full page screenshot". 目前,WebDriver将屏幕截图定义为“整页截图”。 That is, the entire DOM should be represented by the image generated by the save_screenshot method. 也就是说,整个DOM应该由save_screenshot方法生成的图像表示。 The fact that the Chrome driver doesn't generate a screenshot of the full DOM is a bug in the Chrome driver . Chrome驱动程序无法生成完整DOM的屏幕截图这一事实是Chrome驱动程序中的一个错误 So the real answer is that there is no way to generate a screenshot of only the browser view port using WebDriver. 所以真正的答案是没有办法使用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 . 例如,在Windows上,获取桌面窗口的窗口句柄(HWND),捕获桌面图像(使用Windows GetDesktopPrintWindow API)以及使用WebDriver Window API提供的坐标对其进行裁剪非常容易。

I was able to reproduce the problem with Firefox, but Chrome generates resized screenshot. 我能够使用Firefox重现问题,但Chrome会生成重新调整大小的屏幕截图。 My guess is that it is a bug in FirefoxDriver . 我的猜测是它是FirefoxDriver中的一个错误。 Take a look if the problem is already reported in Selenium bug tracker and if not, report it. 看看问题是否已在Selenium bug跟踪器中报告,如果没有,请报告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM