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

后端 未结 4 665
不知归路
不知归路 2020-12-10 05:51

I need to do screenshot of full page using chrome driver, but it makes it partly.

File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType         


        
相关标签:
4条回答
  • 2020-12-10 06:05

    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!

    0 讨论(0)
  • 2020-12-10 06:14

    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);
    
    0 讨论(0)
  • 2020-12-10 06:17

    This is a known bug: https://code.google.com/p/chromedriver/issues/detail?id=294 (Only for Chrome driver, firefox driver works fine)

    0 讨论(0)
  • 2020-12-10 06:26

    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.

    0 讨论(0)
提交回复
热议问题