簡體   English   中英

使用Selenium 3.6截屏-Java-Webdriver

[英]Taking Screenshot using Selenium 3.6 - Java - Webdriver

我無法在我的項目中截圖。 我正在使用Java的Selenium 3.6版本。 這是我的代碼:

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));

不幸的是,“ copyFile”給出了以下錯誤:

The method copyFile(File, File) is undefined for the type FileUtils

我還導入了所有必需的軟件包。

誰能幫我截圖嗎?

導入使用import org.apache.commons.io.FileUtils 這將導入您需要的FileUtils類。

猜猜你導入了錯誤的包

檢查這些包裹應該在那里

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

樣例代碼:

public class Takenscreensshot {
    public static void main(String[] args) throws IOException {
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.google.co.in");
        File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(src, new File("d:/ss.png"));
        driver.close();
    }
}

該錯誤表明所有the "copyFile" is giving an error. It is saying "The method copyFile(File, File) is undefined for the type FileUtils" the "copyFile" is giving an error. It is saying "The method copyFile(File, File) is undefined for the type FileUtils" FileUtils可能在導入中具有多個定義。 因此,解決方案如下:

  • 僅使用:

     import org.apache.commons.io.FileUtils; 

要么

  • 將您的代碼更改為:

     File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); org.apache.commons.io.FileUtils.copyFile(scrFile, new File("C:\\\\tmp\\\\screenshot.png")); 

嘗試

String capture = "window.png";

    try {

        Thread.sleep(3000);

        byte screenshot[] = (byte[])((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);

    FileOutputStream fos = new FileOutputStream(capture);
                fos.write(screenshot);

        }catch (Exception){ }

這對我來說是正確的。

在Selenium 3.6.0中無法使用FiltUtils,因此需要使用FileHandler。

以下是更改

  1. 替換import org.apache.commons.io.FileUtils; import org.openqa.selenium.io.FileHandler;
  2. 替換FileUtils.copyFile(SrcFile, DestFile); FileHandler.copy(SrcFile, DestFile);

現在,用於截屏的代碼如下所示

TakesScreenshot scrShot =(TakesScreenshot)driver;
File SrcFile= scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File(System.getProperty("user.dir")+"\\"+"screenshot.png");
FileHandler.copy(SrcFile, DestFile);

欲了解更多信息,請在這里看看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM