티스토리 뷰

들어가며

  • Selenium을 이용하여 브라우저 화면의 스크린샷을 찍고자 할 때가 있습니다.

 


구현방법

  • 방법은 아래와 같습니다.
  • 각 언어별 구현 방법을 모아놨으니 참고하시면 될 것 같습니다.
//Java 구현방법
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));


//Python 구현방법
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com/')
browser.save_screenshot('screenie.png')

//C# 구현방법
public void TakeScreenshot()
{
    try
    {            
        Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
        ss.SaveAsFile(@"D:\Screenshots\SeleniumTestingScreenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        throw;
    }
}

//JavaScript 구현방법
driver.takeScreenshot().then(function(data){
   var base64Data = data.replace(/^data:image\/png;base64,/,"")
   fs.writeFile("out.png", base64Data, 'base64', function(err) {
        if(err) console.log(err);
   });
});


//Ruby 구현방법
require 'rubygems'
require 'selenium-webdriver'

driver = Selenium::WebDriver.for :ie 
driver.get "https://www.google.com"   
driver.save_screenshot("./screen.png")

참고

 

How to take screenshot with Selenium WebDriver

Does anyone know if it's possible to take a screenshot using Selenium WebDriver? (Note: Not Selenium RC)

stackoverflow.com

 

 

반응형
댓글