如何在不同的时间间隔在Selenium中截取屏幕截图并将其保存在不同的地方错误
How to take screenshots in Selenium at different time intervals and save it at different place error
所以我使用的正是@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());
}
}
始终使用 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());
} }
所以我使用的正是@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());
}
}
始终使用 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());
} }