我正在打开Chromebrowser,并获得exeption "InvocationTargetException"。几天前代码运行正常。这是我的密码
System.setProperty("webdriver.chrome.driver","D:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver=new ChromeDriver();在行"driver=new ChromeDriver();"中,我得到了"InvocationTargetException"异常
发布于 2019-04-25 13:24:48
InvocationTargetException
InvocationTargetException是一个检查过的异常,它包装了被调用的方法或构造函数引发的异常。通过使用反射调用方法,这是一个额外的抽象级别。反射层封装InvocationTargetException中的任何异常。在构造时提供并通过getTargetException()方法访问的“目标异常”现在称为“原因”,可以通过Throwable.getCause()方法以及前面提到的“遗留方法”进行访问。
解决方案
最好的方法是将中的解包到InvocationTargetException中,以获得原始异常。
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();
}最佳做法
根据最佳做法,应遵循以下准则:
tearDown(){}方法中调用tearDown(){}来关闭和销毁WebDriver和WebDriver实例。https://stackoverflow.com/questions/55846217
复制相似问题