Taking screenshot in selenium webdriver in Chrome browser

…衆ロ難τιáo~ 提交于 2019-12-08 13:37:56

问题


I am learning selenium Webdriver. I was trying to take screenshot on chrome browser but I got exception for below code (Note: Same piece of code works on firefox). Kindly help me out to take a screenshot on Chrome and please somebody explain me why below code is not working on Chrome.

public class ScreenShot 
{
    public static void main(String[] args) throws IOException 
    {
        String key  = "webdriver.chrome.driver";
        String value = "./driver/chromedriver.exe";
        System.setProperty(key, value);
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.co.in");
        TakesScreenshot screen = (TakesScreenshot) driver;
        File srcFile = screen.getScreenshotAs(OutputType.FILE);
        File destFile = new File("d:/google.png");
        FileUtils.copyFile(srcFile, destFile);
    }
}       

回答1:


import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public static String captureScreenshot (WebDriver driver, String screenshotName){

    try {
        TakesScreenshot ts = (TakesScreenshot)driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String dest = "/Users/CD6255ABQA/Desktop/Debug Images/" + screenshotName + ".png";
        File destination = new File(dest);
        FileUtils.copyFile(source, destination);
        return dest;
        } 

    catch (IOException e) {return e.getMessage();}
    }

Call it using

String screenpath = captureScreenshot(driver, "ScreenshotName")

Remember to change the file destination in the method.



来源:https://stackoverflow.com/questions/43386398/taking-screenshot-in-selenium-webdriver-in-chrome-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!