java - 为什么我无法在java中使用selenium webdriver截屏?

标签 java selenium screenshot

我正在开发一个使用 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/

相关文章:

Java jbutton 不可见

Docker 容器中的 Selenium,我们可以截屏吗

java - 在 TestNG 中获取方法调用的 NullPointerException

javascript - Python selenium 选择一个不可见的复选框

ASP.NET/ActiveX/Silverlight 屏幕捕获

java - 重复 void 方法

java - 取消刷新尝试 : org. springframework.beans.factory.UnsatisfiedDependencyException: Spring Boot

java - 排除数组的索引 0 作为输出选项?

ios - 如何获得高质量的 iOS 屏幕截图?

python - 在 Python 中捕获一个窗口