在Chrome浏览器中的Selenium WebDriver中截屏 [英] Taking screenshot in selenium webdriver in Chrome browser

查看:237
本文介绍了在Chrome浏览器中的Selenium WebDriver中截屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Selenium Webdriver.我试图在chrome浏览器上截屏,但以下代码却异常(注意:同一段代码在firefox上有效).请帮助我在Chrome上截屏,请有人向我解释为什么下面的代码无法在Chrome上运行.

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);
    }
}       

推荐答案

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();}
    }

使用

String screenpath = captureScreenshot(driver, "ScreenshotName")

记住要在方法中更改文件目标.

Remember to change the file destination in the method.

这篇关于在Chrome浏览器中的Selenium WebDriver中截屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆