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;到更高的進口訂單,它工作得很好。謝謝。 –

相關問題