获得"InvocationTargetException".行驱动程序= new ChromeDriver()上的异常; [英] Getting the "InvocationTargetException" exception on the line driver=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
中.构造时提供并通过 Throwable.getCause()
方法,以及上述旧版方法".
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:
- 将 ChromeDriver 升级到当前的 ChromeDriver v74.0.3729 .6 级.
- 保持 Chrome 版本处于 Chrome v74 级别. (根据ChromeDriver v74.0.3729.6发行说明)
- 以非root用户用户的身份执行
@Test
. - 始终在
tearDown(){}
方法中调用driver.quit()
以关闭&优雅地销毁 WebDriver 和 Web Client 实例.
- 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()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
这篇关于获得"InvocationTargetException".行驱动程序= new ChromeDriver()上的异常;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!