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

后端 未结 1 705
孤城傲影
孤城傲影 2020-12-07 05:07

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



        
相关标签:
1条回答
  • 2020-12-07 06:05

    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.
    0 讨论(0)
提交回复
热议问题