所以我使用@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());
}
我不知道您的代码是如何设置的,但是我进行了一次测试,没有问题。它导航到Google并间隔三秒拍摄三个屏幕截图。我相信您可能有导入或依赖问题。
这是示例:
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());
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句