我从另一个包导入这个类并尝试调用这个方法,但它不起作用。
当我在同一个类中创建此方法并调用它时,它正在工作。
private void getScreenshot() throws IOException
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY/hh-mm-ssaa");
String destfile = dateFormat.format(new Date()) + ".png";
FileUtils.copyFile(scrFile, new File("D:\\workspace\\Firewall\\Resources\\ScreenShots\\"+destfile));
}
最佳答案
我猜主要原因是你导入了错误的库。查看:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
如果您的库相同,请尝试使用我的方法:
public class TakeScreenshot {
WebDriver driver;
public TakeScreenshot(WebDriver driver){
this.driver = driver;
}
public void ScreenShot(String nameTc)
{
// Take screenshot and store as a file format
File src= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
// now copy the screenshot to desired location using copyFile //method
FileUtils.copyFile(src, new File("bin/" + nameTc + ".png"));
}
catch (IOException e)
{
System.out.println(e.getMessage());
}} }
关于java - 使用 Selenium 截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41672961/