繁体   English   中英

如何使用Selenium Web Drivver拍摄当前窗口的屏幕截图或模拟打印屏幕

[英]How to take screenshot of Current window or simulate print screen using selenium web drivver

我正在创建一个自动化工具,该工具将有助于拍摄网页的屏幕快照,我正在使用Selenium Web驱动程序(Java),目前,我能够使用以下代码来拍摄整个网页的屏幕快照

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile, new File("full.jpeg"));

但是我想用硒单独拍摄当前窗口的屏幕快照(我想模拟打印屏幕功能的动作),请指导我如何单独拍摄当前网页的屏幕快照。

问候,Vignesh

看看我的实用程序代码

public static String captureScreen() {
    //get your driver instance        
    try {
        File source = ((TakesScreenshot)
                driver).getScreenshotAs(OutputType.FILE);
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat(
                "yyyy/MMM/dd HH:mm:ss");
        String dateN = formatter.format(currentDate.getTime()).replace("/","_");
        String dateNow = dateN.replace(":","_");
        String snapShotDirectory =  /*your snapshot folder path*/+ dateNow;

        File f = new File(snapShotDirectory);
        if(f.mkdir()){
        path = f.getAbsolutePath() + "/" + source.getName();
        FileUtils.copyFile(source, new File(path)); 
        }
    }
    catch(IOException e) {
        path = "Failed to capture screenshot: " + e.getMessage();
    }
    return path; 
}

只要在要拍摄快照的地方调用此方法即可。

WebDriver中没有支持此功能的东西,但是您可以使用Java Robots截屏: http : //docs.oracle.com/javase/7/docs/api/java/awt/Robot.html#createScreenCapture%28java.awt 。矩形%29

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM