How to get screenshot of full webpage using Selenium and Java?

前端 未结 9 750
耶瑟儿~
耶瑟儿~ 2020-12-29 08:17

How to take a screenshot of the entire web page (full-page screenshot), not only partial (top-to-bottom) using Selenium WebDriver?

My code:

9条回答
  •  盖世英雄少女心
    2020-12-29 08:48

    It is possible to start Chrome in headless mode and then set the window size to the same height of the page body.

    Since the height of your headless browser is not dependent on your monitor size you can use this headless mode to get full length screenshots as they are rendered in the Chrome browser.

    The top answer for Headless Chrome run with selenium seems to show the right way to start Chrome in headless mode with java. I work with python bindings but the answer looks pretty similar to what I do.

    This is the relevant part. This code should go before you start an instance of ChromeDriver.

    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.addArguments("--headless");
    
    ChromeDriver driver = new ChromeDriver(chromeOptions);
    

    If running chrome in a headless browser is not a viable option then you'll have to use Firefox or IE. Both of these browsers will give you a full length screenshot

提交回复
热议问题