Register to Better Coder! It's free.
Enjoy the premium features and succeed at every job interview.

How to capture a screenshot using Selenium WebDriver?

Experience Level: Medior
Tags: Quality Assurance (QA)Selenium

Answer

Answer

Capturing screenshot is easy. Just use the method GetScreenshot() of the Selenium driver. if you are using interface IWebDriver, you will need to cast the driver instance to ITakesScreenshot interface.

C#

try {
  var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
  var screenshotFileName = @"C:\Tests\Screenshots\Screenshot.jpg";
  screenshot.SaveAsFile(screenshotFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception ex) {
  Console.WrieLine(ex.ToString());
}

Note that the code can fail while generationg screenshot or saving the screenshot to a filesystem so it's always a good practice to use try/catch.

Related Quality Assurance (QA) job interview questions

Comments

No Comments Yet.
Be the first to tell us what you think.
Selenium training
Selenium training

Are you learning Quality Assurance (QA) ? Try our test we designed to help you progress faster.

Test yourself

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e
LaceJaguar65
e