Getting the “InvocationTargetException” exception on the line driver=new ChromeDriver();

穿精又带淫゛_ 提交于 2019-12-20 04:26:20

问题


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();

At the line "driver=new ChromeDriver();" I am getting the "InvocationTargetException" Exception


回答1:


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."

Solution

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.


来源:https://stackoverflow.com/questions/55846217/getting-the-invocationtargetexception-exception-on-the-line-driver-new-chromed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!