Selenium WebDriver 在调用 getScreenshotAs() 时抛出 TimoutException

Selenium WebDriver throwing TimoutException while invoking getScreenshotAs()

这是我的代码。

public static void test1() throws IOException {
    System.setProperty("webdriver.chrome.driver", "data/chromedriver.exe");
    drive = new ChromeDriver();
    drive.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    try {
        drive.get("http://youtube.com");
    }catch(TimeoutException e) {
        printSS();
    }

}

public static void printSS() throws IOException{
    String path = "logs/ss/";
    File scrFile = ((TakesScreenshot)drive).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File(path + "asdasdas" + ".jpg"));
}

当 driver.get() 抛出 TimeoutException 时,我想在浏览器上截图。

但是当抛出 TimeoutException 时,printSS() 中的 getScreenshotAs() 不会截取屏幕截图,因为会抛出另一个 TimeoutException。

为什么 getScreenshotAs() 抛出 TimeoutException 以及如何在浏览器上截屏

P.S.: 增加pageLoadTimeout时间不是我想要的答案

使用 Selenium 3.xChromeDriver 2.36Chrome 65.x 您需要提及您打算存储 截图.

我把你的代码做了一些小的修改如下:

  • driver 声明为 WebDriver 实例为 static 并添加 @测试注解。
  • pageLoadTimeout 减少到 2 秒以有目的地引发 TimeoutException
  • String path 位置 更改为项目范围内的 sub-directory 如下:

    String path = "./ScreenShots/";
    
  • 添加日志为:

    System.out.println("Screenshot Taken");
    
  • 这是代码块:

     package captureScreenShot;
    
     import java.io.File;
     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.TimeoutException;
     import org.openqa.selenium.WebDriver;
     import org.openqa.selenium.chrome.ChromeDriver;
     import org.testng.annotations.Test;
    
     public class q49319748_captureScreenshot 
     {
    
        public static WebDriver drive;
    
        @Test
        public static void test1() throws IOException {
            System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
            drive = new ChromeDriver();
            drive.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
            try {
                drive.get("http://youtube.com");
            }catch(TimeoutException e) {
            printSS();
            }
    
        }
    
        public static void printSS() throws IOException{
            String path = "./ScreenShots/";
            File scrFile = ((TakesScreenshot)drive).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(scrFile, new File(path + "asdasdas" + ".jpg"));
            System.out.println("Screenshot Taken");
        }
     }
    
  • 控制台输出:

     [TestNG] Running:
       C:\Users\username\AppData\Local\Temp\testng-eclipse--153679036\testng-customsuite.xml
    
     Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 42798
     Only local connections are allowed.
     Mar 16, 2018 5:37:59 PM org.openqa.selenium.remote.ProtocolHandshake createSession
     INFO: Detected dialect: OSS
     Screenshot Taken
    PASSED: test1
    
  • 截图:


参考

您可以在 How to take screenshot with Selenium WebDriver

中找到详细的讨论

问题是,当 Selenium 等待页面完成加载时,它无法接受任何其他命令。这就是为什么当您尝试截取屏幕截图时它也会从异常处理程序中抛出 TimeoutException 的原因。 我看到的唯一选择是不通过 Selenium 截取屏幕截图,而是使用其他方式截取整个桌面的屏幕截图。我用 C# 写过这样的东西,但我很确定你也可以在 Java 中找到一种方法。