How can I take a screenshot with Selenium WebDriver?

后端 未结 30 2657
不知归路
不知归路 2020-11-21 07:48

Is it possible to take a screenshot using Selenium WebDriver?

(Note: Not Selenium Remote Control)

30条回答
  •  面向向阳花
    2020-11-21 08:23

    Java

    public void captureScreenShot(String obj) throws IOException {
        File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File("Screenshots\\" + obj + "" + GetTimeStampValue() + ".png"));
    }
    
    public String GetTimeStampValue()throws IOException{
        Calendar cal = Calendar.getInstance();
        Date time = cal.getTime();
        String timestamp = time.toString();
        System.out.println(timestamp);
        String systime = timestamp.replace(":", "-");
        System.out.println(systime);
        return systime;
    }
    

    Using these two methods you can take a screen shot with the date and time as well.

提交回复
热议问题