Selenium: chrome driver makes screenshot just of visible part of page

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

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);


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!