即使在导入所需的jar文件后,我在下面两行代码中收到错误 [英] Even after importing the required jar files I am getting an error in the below two line of code

查看:98
本文介绍了即使在导入所需的jar文件后,我在下面两行代码中收到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包tenis2;



import java.io.IOException;

import java.util.concurrent.TimeUnit;



import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org .openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;



公共类截图{

public static void main(String [] args)抛出IOException {



WebDriver driver = new ChromeDriver();

driver.manage()。timeouts()。implicitlyWait(60,TimeUnit.SECONDS);

driver.get(http: //www.flipkart.com/);

driver.manage()。window()。maximize();

文件scrFile =((TakesScreenshot)驱动程序) .getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(scrFile,new File(D:\\ new\\ screenshot1.png),true);



driver.quit();

}

}



我的尝试:



上面的代码实际上包含截取屏幕截图的脚本。我实际上是从其中一个站点获取此代码。但是当我尝试使用相同的代码时,它会在以下两行中抛出错误,说添加强制转换为文件。任何人都可以帮我解决这个问题



文件scrFile =((TakesScreenshot)驱动程序).getScreenshotAs(OutputType.FILE);

FileUtils。 copyFile(scrFile,new File(D:\\ new\\screenshot1.png),true);



这两行是

package tenis2;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class screenshot {
public static void main(String[] args)throws IOException{

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.get("http://www.flipkart.com/");
driver.manage().window().maximize();
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\new\\screenshot1.png"), true);

driver.quit();
}
}

What I have tried:

The code above actually contains the scripts to take a screenshot. I actually took this code from one of the sites. But when the same code is being tried by me it throws an error in the following two lines saying "add cast to file". Can anyone help me out with this

File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\new\\screenshot1.png"), true);

These are the two line

推荐答案

编译器告诉你从getScreenshotAs方法返回的对象不是File,这是你试图分配给它的。



您可能想要使用这样的演员强制执行作业:

The compiler is telling you that the object returned from the getScreenshotAs method is not a File, which is what you're trying to assign it to.

You might be tempted to force the assignment using a cast like this:
File scrFile = (File) ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\new\\screenshot1.png"), true);



然而,这不是正确的解决方案。看看代码的顶部(做好了发布所有内容!)我们可以看到你有这个导入:


However that's not the right solution. Look right at the top of your code (well done for posting all of it!) where we can see that you have this import:

import org.apache.commons.io.FileUtils;



您需要:


Whereas you need:

import java.io.File;



我认为如果你改变这个导入它应该编译无怨言,无需上述演员。


I think that if you change this import it should compile without complaint and without need for that cast mentioned above.


这篇关于即使在导入所需的jar文件后,我在下面两行代码中收到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆