可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I need to do screenshot of full page using chrome driver, but it makes it partly.
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
The screenshot looks as visible rectangle with correct information and big black area below.
回答1:
This is a known bug: https://code.google.com/p/chromedriver/issues/detail?id=294 (Only for Chrome driver, firefox driver works fine)
回答2:
Might worth trying to use this library:
https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver
To make a full page screenshot:
Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save();
(it uses scroll-and-stitch method)
Sources on github https://github.com/assertthat/selenium-shutterbug
Provides ability to make full page screenshot in Chrome and some other extended features, tested on Windows and OS X.
Successfully using on my current project.
回答3:
you need to use
load 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);
Command to download full page screenshot by this command
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(); })
you may call this script using javascriptexecutor and get desired results as download of the image would launch automatically to your default download location and you may change file name with an input argument of the javascriptexecutor command of the selenium.
hope this helps!
回答4:
I know this is an old thread, but I wanted to show use of Selenium's ITakesScreenshot.
using OpenQA.Selenium; using System.Drawing.Imaging; ((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(@"YourImageNameHere.png", ImageFormat.Png);