Close the chromedriver process after the webdriver is executed

Background: The login part of a website uses selenium, but after multiple logins, it is found that a lot of chromedriver.exe processes remain in the process. After the project was packaged into a jar package, it was put on another machine to run, and it was found that it started to freeze soon after, and even the browser window could not be popped up, so I wondered if it was caused by too many residual processes of chromedriver.exe. And because the individual has obsessive-compulsive disorder, this decision is optimized.

Original code:

	System.setProperty("webdriver.chrome.driver", webDriverPath);
        WebDriver driver = new ChromeDriver();driver.get("xxxxxxxxxx");
        
Close is done with driver.quit(), not driver.close(). The two differences are as follows:

close will only close the current window

quit will launch the driver and do not close all associated windows


Go to Baidu later, and paste the relevant URL and solution code below.

https://www.testwo.com/blog/6931

Final solution code:

  ChromeDriverService service = new ChromeDriverService.Builder().usingDriverExecutable(new File(webDriverPath)).usingAnyFreePort().build();
        try {
            service.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.setProperty("webdriver.chrome.driver", webDriverPath);
//        WebDriver driver = new ChromeDriver();
        WebDriver driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
        driver.get("xxxxxxxx");

Code to close:

driver.quit();
service.stop();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325555593&siteId=291194637