SeleniumFullPageScreenShot

CSharp · Console

/Program.cs

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Collections.Generic;

public class Program
{
	public static void Main()
	{
		ChromeDriver driver = new ChromeDriver();
		string page_to_screenshot = "https://google.com";
		driver.Url = page_to_screenshot;
		
		//Dictionary will contain the parameters needed to get the full page screen shot
		Dictionary<string, Object> metrics = new Dictionary<string, Object>(); 
		metrics["width"] = driver.ExecuteScript("return Math.max(window.innerWidth,document.body.scrollWidth,document.documentElement.scrollWidth)");
		metrics["height"] = driver.ExecuteScript("return Math.max(window.innerHeight,document.body.scrollHeight,document.documentElement.scrollHeight)");
		metrics["deviceScaleFactor"] = (double)driver.ExecuteScript("return window.devicePixelRatio");
		metrics["mobile"] = driver.ExecuteScript("return typeof window.orientation !== 'undefined'");
		//Execute the emulation Chrome Command to change browser to a custom device that is the size of the entire page
		driver.ExecuteChromeCommand("Emulation.setDeviceMetricsOverride", metrics);
		//You can then just screenshot it as it thinks everything is visible
		string path_to_save_screenshot = @"C:/temp/temp.png";
		driver.GetScreenshot().SaveAsFile(path_to_save_screenshot, ScreenshotImageFormat.Png);
		//This command will return your browser back to a normal, usable form if you need to do anything else with it.
		driver.ExecuteChromeCommand("Emulation.clearDeviceMetricsOverride", new Dictionary<string, Object>());
		driver.Close();
	}
}
;