我需要使用 chrome 驱动程序对整页进行截图,但这只是部分截图。
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
屏幕截图看起来像可见的矩形,下面有正确的信息和大的黑色区域。
我需要使用 chrome 驱动程序对整页进行截图,但这只是部分截图。
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
屏幕截图看起来像可见的矩形,下面有正确的信息和大的黑色区域。
这是一个已知的错误:https ://code.google.com/p/chromedriver/issues/detail?id=294 (仅适用于 Chrome 驱动程序,firefox 驱动程序工作正常)
可能值得尝试使用这个库:
https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver
要制作整页屏幕截图:
Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save();
(它使用滚动和缝合方法)
github 上的来源https://github.com/assertthat/selenium-shutterbug
提供在 Chrome 和一些其他扩展功能中制作全页截图的能力,在 Windows 和 OS X 上进行了测试。
成功用于我当前的项目。
你需要使用
加载 html2canvas.js
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://github.com/niklasvh/html2canvas/releases/download/0.5.0-alpha1/html2canvas.js';
document.head.appendChild(script);
通过此命令下载整页截图的命令
html2canvas(document.body).then(function(canvas) {
var a = document.createElement('a');
// toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
a.download = 'somefilename.jpg';
a.click();
})
您可以使用 javascriptexecutor 调用此脚本并获得所需的结果,因为图像的下载将自动启动到您的默认下载位置,您可以使用 selenium 的 javascriptexecutor 命令的输入参数更改文件名。
希望这可以帮助!
我知道这是一个旧线程,但我想展示 Selenium 的 ITakesScreenshot 的使用。
using OpenQA.Selenium;
using System.Drawing.Imaging;
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(@"YourImageNameHere.png", ImageFormat.Png);