Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 499
Apurv Chatterjee Avatar asked Jan 05 '23 04:01

Apurv Chatterjee


1 Answers

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. geckodriver, selenium-chromedriver, selenium-iedriver through os specific methods as follows:

  • Java Solution(Windows):

    import java.io.IOException;
    
    public class Kill_ChromeDriver_GeckoDriver_IEDriverserver 
    {
        public static void main(String[] args) throws Exception 
        {
            Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe /T");
            Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe /T");
            Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe /T");
        }
    }
    
  • Python Solution(Windows):

    import os
    os.system("taskkill /f /im geckodriver.exe /T")
    os.system("taskkill /f /im chromedriver.exe /T")
    os.system("taskkill /f /im IEDriverServer.exe /T")
    
  • Python Solution(Cross Platform):

    import os
    import psutil
    
    PROCNAME = "geckodriver" # or chromedriver or IEDriverServer
    for proc in psutil.process_iter():
        # check whether the process name matches
        if proc.name() == PROCNAME:
            proc.kill()
    
like image 190
undetected Selenium Avatar answered Jan 31 '23 20:01

undetected Selenium