1

所以我使用@Pawel_Awdmski的这个确切代码。我得到(OutputType.FILE)下的错误;说FILE不能解决或不在现场。为什么它会给我那个错误?如何在不同的时间间隔在Selenium截图并将其保存在不同的位置错误

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

没有能够看到所有的代码,我说你已经导入了错误的'OutputType'或忘记导入它在所有。 'import org.openqa.selenium.OutputType;' – PizzaFrog

+1

你必须导入'import java.io.File;或者导入java.io。*;'类的顶部。 – gihan

+0

我现在更新我的导入。我仍然得到错误。当我尝试iimport org.openqa.selenium.OutputType;它说删除未使用的进口 –

回答

2

我不知道你的代码是如何设置的,但是我做了一个没有问题的测试。它导航到谷歌并相隔三秒钟取得三个截图。 我相信你可能有导入或依赖问题。

这里是例子:

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()); 
    } 

}

+0

Ty!我想我知道我做错了什么,只需要移动OutputType;到更高的进口订单,它工作得很好。谢谢。 –

相关问题