javaseleniumselenium-webdriverwebdriverselenium-chromedriver

Selenium initiated ChromeDriver keeps running in background


I am not able to completely delete a project, because the chromedriver instance is running in the background, even when there is no code is being executed. Please see the below image. Task Manager Process View

The error I get while the project deletion is:Eclipse error while project deletion

Take the below code for instance:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

    public static void main(String[] args) throws Exception {

        String url = "https://www.google.com";
        System.setProperty("webdriver.chrome.driver", "src/driver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(url);
        System.out.println(driver.getTitle());

    }
}

Try running this code, say 5 times, and there will be 5 background running instances. Tried restarting Eclipse, with no luck.

I understand this is happening because I am not writing this line:

driver.close();

But normally, when the main-thread dies, all supporting running instances should die with it.

Is this a known issue, or I am scripting something wrong.

Thanks in advance.


Solution

  • You need to take care of a couple of things as follows:

    1. Try running this code, say 5 times, and there will be 5 background running instances: This is because each time you have opened a new driver instance which you have never released.

    2. Tried restarting Eclipse, with no luck: Its worth mentioning that Eclipse is an IDE (Integrated Development Environment) which helps us to construct programming steps through a programming language. So through Eclipse we write programs/steps for Selenium to open/close a browser session and quit a driver session. But Eclipse have no control over the execution done by Selenium in-terms of open/close a browser session and quit a driver session. Hence restarting Eclipse would make no difference.

    3. driver.close(): This command will help you only to Close the browser session/window on which currently the driver have the focus.

    4. when the main-thread dies, all supporting running instances should die with it: You are right. It is the WebDriver instance (i.e. driver - as per your code) which opens all the browser sessions every-time. So when your program ends you should always call driver.quit() which Closes all browser windows, quits the driver and safely ends the session.


    However, at any point of time you can forcefully kill all the instances of WebDriver variants e.g. , , through specific methods as follows: