所以我正在使用@Pawel_Awdmski的确切代码。我在(OutputType.FILE)下得到错误;说FILE无法解析或不在现场。为什么它会给我这个错误?
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.apache.commons.io.FileUtils;
import org.eclipse.jetty.server.Response.OutputType;
import org.openqa.selenium.By;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import java.util.NoSuchElementException;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.io.*;
public void screenShot() throws IOException, InterruptedException
{
File scr=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File dest= new File("filPath/screenshot_"+timestamp()+".png");
FileUtils.copyFile(scr, dest);
Thread.sleep(3000);
}
public string timestamp() {
return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
}
答案 0 :(得分:3)
我不知道你的代码是如何设置的,但是我做了一个没有问题的测试。它导航到谷歌并分三秒拍摄三张截图。 我相信你可能有导入或依赖问题。
以下是示例:
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) throws Exception {
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://google.com");
screenShot(driver);
screenShot(driver);
screenShot(driver);
}
public static void screenShot(FirefoxDriver driver) throws IOException, InterruptedException {
File scr=(driver).getScreenshotAs(OutputType.FILE);
File dest= new File("filPath/screenshot_"+timestamp()+".png");
FileUtils.copyFile(scr, dest);
Thread.sleep(3000);
}
public static String timestamp() {
return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
}
}
答案 1 :(得分:0)
总是使用maven,以一种简单的方式添加依赖项是一个好习惯
public class screenshots {
public static void main(String[] args) throws Exception {
WebDriver driver;
//put correct path for Gecko driver
System.setProperty("webdriver.gecko.driver", "G:/Selenium Driver/Gecko/geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://google.com");
screenShot(driver);
screenShot(driver);
screenShot(driver);
}
public static void screenShot(WebDriver driver) throws IOException, InterruptedException {
File scr = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File dest = new File("C:/Users/dayan/screenshot_" + timestamp() + ".png");
FileUtils.copyFile(scr, dest);
Thread.sleep(3000);
}
public static String timestamp() {
return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
} }