Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Take screenshot of a file opened in Selenium browser

I am downloading a pdf file with Selenium browser and I need a screenshot of it. The simplest method that I thought was to open it in the browser (Chrome) and take a screenshot with the driver. But, the issue is that the screenshot is empty, with a gray background

Am I missing something? Isn’t Selenium Chromedriver able to take a screenshot of a file opened in browser?

Some code that I use:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

driver.SwitchTo().NewWindow(WindowType.Tab);
                        
Thread.Sleep(1000);

driver.Navigate().GoToUrl(file);

ITakesScreenshot screenshot = driver as ITakesScreenshot;

Screenshot screenshotObj = screenshot.GetScreenshot();

screenshotObj.SaveAsFile(filePath, ScreenshotImageFormat.Png);

>Solution :

Remember that Selenium operates asynchronously in real-time. GoToUrl returns long before the file has been received. You might add a multi-second sleep and see if that helps things.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading