0

When I am trying to close the chrome browser using driver.quit() and driver.close(), browser is getting closed but chromedriver.exe process is not getting closed. How can we remove these processes while we are closing the browser. I am trying in java.

2

Yeah, it can be a general issue as browsers (especially Chrome) don't always like closing their driver processes.

You can put the below in the onFinish all tests method which kills them:

String systemType = System.getProperty("os.name").toLowerCase();

        if (systemType.contains("win")) {
            try {
                // Selenium drivers don't always close properly, kill them
                System.out.println("Close one or more driver exe files");
                Runtime.getRuntime().exec("taskkill /f /im chromedriver.exe");
                Runtime.getRuntime().exec("taskkill /f /im operadriver.exe");
                Runtime.getRuntime().exec("taskkill /f /im geckodriver.exe");
                Runtime.getRuntime().exec("taskkill /f /im IEDriverServer.exe");
            } catch (IOException e) {
                System.out.println("Failed to close one or more driver exe files");
            }
        }
| improve this answer | |
  • How can we kill processes which are abandoned ? Without disturbing the running processes – sri Oct 25 '19 at 4:49
0

I do something similar (albeit in C#):

using System.Diagnostics;

namespace <Redacted for privacy>.Helper_Classes
{
    public class KillDriverObjects
    {
        public void KillOpenChromeDrivers()
        {
            Process[] killChrome = Process.GetProcessesByName("chromedriver.exe");

            foreach (var process in killChrome)
            {
                process.Kill();
            }
        }

        public void KillOpenIEDrivers()
        {
            Process[] killInternetExplorer = Process.GetProcessesByName("iexplore.exe");

            foreach (var process in killInternetExplorer)
            {
                process.Kill();
            }
        }
    }
}

NOTE: I would use this approach very sparingly as it will kill all of the processes, not just the one you are targeting.

| improve this answer | |
0

If you are using WebDriver directly (e.g. new WebDriver()), I recommend replacing with use of Selenium DriverService (e.g. new ChromeDriverService()). Though I prefer to explicitly close WebDrivers prior to stopping the DriverService, the following demonstrates stopping the service reliably closes all related WebDrivers.

@Test
public class ExampleTests {
    private final DriverService service;

    {
        service = new ChromeDriverService.Builder()
                .usingDriverExecutable(new File("./src/main/resources/chromedriver.exe"))
                .usingAnyFreePort()
                .build();
    }

    @BeforeClass
    public void initializeTesting() {
        try {
            service.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFirst() {
        RemoteWebDriver driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
    }

    @Test
    public void testSecond() {
        RemoteWebDriver driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
        driver = new RemoteWebDriver(service.getUrl(), new ChromeOptions());
    }

    @AfterClass
    public void terminateTesting() {
        service.stop();
    }
}

WebDrivers orphaned by Exceptions thrown during test execution will still need to be addressed.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.