获得"InvocationTargetException".行驱动程序= new ChromeDriver()上的异常; [英] Getting the "InvocationTargetException" exception on the line driver=new ChromeDriver();

查看:97
本文介绍了获得"InvocationTargetException".行驱动程序= new ChromeDriver()上的异常;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开Chromebrowser,并获取示例"InvocationTargetException".该代码几天前已正常运行.这是我的代码

I am opening the Chromebrowser, and getting the exeption "InvocationTargetException". The code was running properly few days ago. Here is my code

System.setProperty("webdriver.chrome.driver","D:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();

"driver=new ChromeDriver();"行,我得到了"InvocationTargetException"异常

推荐答案

InvocationTargetException

InvocationTargetException 是一个检查的异常,该异常包装了由调用的方法或构造函数引发的异常.通过反射调用方法是一种额外的抽象层次.反射层将所有异常包装在InvocationTargetException中.构造时提供并通过

InvocationTargetException

InvocationTargetException is a checked exception that wraps an exception thrown by an invoked method or constructor. It is an extra level of abstraction by calling the method with reflection. The reflection layer wraps any exception in an InvocationTargetException. The "target exception" that is provided at construction time and accessed via the getTargetException() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."

最好的方法是展开 InvocationTargetException中的原因以获取原始异常.

The best approach would be to unwrap the cause within the InvocationTargetException to get the the original exception.

try {

        System.setProperty("webdriver.chrome.driver","D:\\Automation\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

} catch (InvocationTargetException e) {
        // the real cause
        e.getCause().printStackTrace();

} catch (Exception e) {
        // generic exception handling
        e.printStackTrace();
}


最佳做法

按照最佳做法,请遵循以下准则:


Best Practice

As per the best practices follow the below guidelines:

  • Upgrade ChromeDriver to current ChromeDriver v74.0.3729.6 level.
  • Keep Chrome version at Chrome v74 levels. (as per ChromeDriver v74.0.3729.6 release notes)
  • Execute your @Test as non-root user.
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

这篇关于获得"InvocationTargetException".行驱动程序= new ChromeDriver()上的异常;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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