为什么我不能在 java 中使用 selenium webdriver 截屏?

Why I can't take a screenshot using selenium webdriver in java?

我正在 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);

为什么会这样?

错误已解决。错误是 TakesScreenshot 无法解析为变量和标记“)”上的语法错误。所以,我删除了“)”。所以正确的语法是

    File srcFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

您可以如下使用:

FileUtils.copyFile(((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE), new File("E:\Android-workspace\Test1\src\ScreenShot\screenshot.png"));