我正在开发一个使用 java 中的 Selenium webdriver 进行回归测试的项目。我编写了以下代码来截图。
package SelTest;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class takeScreenShottest {
public WebDriver driver;
@Test
public void openBrowser() throws Exception{
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.google.com");
try{
driver.findElement(By.id("testing")).sendKeys("test");
}catch(Exception e){
System.out.print("I am in Exception");
getscreenshot();
}
}
public void getscreenshot() throws Exception{
File scrFile=((TakesScreenshot)).driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("E:\\Android-workspace\\Test1\\src\\ScreenShot\\screenshot.png"));
}
public static void main(String[] args) throws Exception{
takeScreenShottest ts = new takeScreenShottest();
ts.openBrowser();
}
}
我收到错误
File scrFile=((TakesScreenshot)).driver).getScreenshotAs(OutputType.FILE);
为什么会发生这种情况?
最佳答案
您可以按如下方式使用它:
FileUtils.copyFile(((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("E:\\Android-workspace\\Test1\\src\\ScreenShot\\screenshot.png"));
关于java - 为什么我无法在java中使用selenium webdriver截屏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35452201/