1

I'm looking into having my program open a Selenium WebDriver browser, go into a specific page and take a screenshot of it. However, the window needs to be minimized in order to not interfere with our users too much. I have the WebDriver working fine, I just need to get a screenshot of it

Is this even possible to do so?

I looked into this, but it's far to advanced for me to understand what they're doing: Saving a screenshot of a window using C#, WPF, and DWM

Any help is appreciated!

wads
  • 123
  • 11
  • 1
    I believe I have done something similar to [this answer](https://stackoverflow.com/a/47506830/13885000) for this problem in the past. – cdbullard Jun 20 '22 at 14:14

1 Answers1

1

Yes, it is possible to do using using OpenQA.Selenium.Chrome:

private ChromeDriver _driver;

public void Run()
{
    InitWebDriverInMinimizedWindow();

    // Your code that uses _driver

    TakeScreenshot(@"D:\Screenshots\screenshot001.jpg");
}

public void InitWebDriverInMinimizedWindow()
{
    var options = new ChromeOptions();

    // launch a browser without creating a window
    options.AddArgument("headless");
    _driver = new ChromeDriver(options);
}

public void TakeScreenshot(string fileName)
{
    try
    {
        Screenshot screenshot = ((ITakesScreenshot)_driver).GetScreenshot();
        screenshot.SaveAsFile(fileName, ScreenshotImageFormat.Jpeg);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        throw;
    }
}
Gregory L
  • 376
  • 3
  • 5
  • This did the job! However, I can see when I open the picture that it's a cut image in the "center of the screen" ( [see picture](https://imgur.com/a/JFBKogu) ). Is there a way to have it maximized, as there will be things missing, if the screen isn't maximized. I tried using `driver.Manage().Window.Maximize();` but it didn't work and I assume it's because there isn't actually a window to maximize – wads Jun 21 '22 at 06:17