Selenium webdriver does not quit chrome driver
Asked Answered
W

9

10

I'm using Selenium webdriver but it don't quit chrome and chrome driver properly . Some of processes staid runner.

code for quitting chrome :

 driver.quit();

code for starting chrome :

 System.setProperty("webdriver.chrome.driver","/<path to chrome driver>/chromedriver");
 ChromeOptions options = new ChromeOptions();
 options.setBinary(new File("/<path to chrome >/google-chrome"));
 driver = new ChromeDriver(options);

Chrome driver version :2.9.248304 Chromium version :40.0.2214.115 Selenium version :2.32 OS: Linux java.version: 1.7.0_71

Thanks in advance , Naira

Whiffet answered 18/3, 2015 at 13:36 Comment(5)
What is the error you are getting?Surmount
Why are you using selenium 2.32? - it is very very old, upgrade to the latest (2.45).Altimetry
How did you guarantee that your driver.quit() actually got executed?Gomphosis
if it didn't quit chrome then its sure it didn't encountered driver.quit or u might have lost reference to the chrome window.Cankered
There is no errors just chrome processes stays . I will try to change selenium version . As I understand driver.quit kill chrome processes , but it didn't kill properly or may be there is problem wit selenium version ?Whiffet
W
-13

The problem was with driver.quit() method only for Chrome. The driver quit didnt work properly it didnt kill all processes of chrome (including child processes). What I did. I changed Selenium jar codes to fix this for my project , unfortunatly I cant share my code caus of project rules which dont allow to share any type of code .

Whiffet answered 24/11, 2017 at 19:0 Comment(1)
You're bound by IP agreements, sure. But, there is nothing wrong in explaining the logic, if you have one. That's not IP.Dx
O
5

Are you executing your driver.quit() within a finally block?

System.setProperty("webdriver.chrome.driver","/<path to chrome driver>/chromedriver");
ChromeOptions options = new ChromeOptions();
options.setBinary(new File("/<path to chrome >/google-chrome"));
driver = new ChromeDriver(options);
try
{
    //automated steps
}
finally
{
    driver.quit();
}
Olfe answered 19/3, 2015 at 16:10 Comment(3)
Driver quit work . I see it by logs and when i run local to . But processes collected again and againWhiffet
Yes . Also I use selenium 2.45 now. But processes still collect . What can I do ?Whiffet
You should also put the driver.quit() into a second try {} catch {} block! It may throw.Mesarch
C
4

I solved them this way:

import os

os.system('killall chrome')

It is useful if you don't use Google Chrome for something else.

Cornett answered 17/4, 2020 at 5:19 Comment(1)
os.system returned 0 but I still see the chromedriver running in htopReginaldreginauld
S
4

It works fine for me if I use

driver.close();
driver.quit();
Sturtevant answered 29/6, 2020 at 5:24 Comment(0)
F
3

So, nothing worked for me. What I ended up doing was setting a unique ID on my addArguments to launch chromedriver, then when I want to quit I do something like this:

opts.addArguments(...args, 'custom-pid-' + randomId());

Then to make sure it quits:

await this.driver.close()
await this.driver.quit()

spawn(`kill $(ps aux | grep ${RANDOM_PID_HERE} | grep -v "grep" | awk '{print $2}')`)
Femi answered 4/3, 2021 at 4:12 Comment(0)
B
1

1) Get the driver as singleton

@Singleton
class BrowserInstance {

ChromeDriver getDriver(){
    ChromeOptions options = new ChromeOptions()
    options.addArguments("--headless --disable-gpu")
    return new ChromeDriver(options)
   }
}

2) Use Close and quit in finally block

 finally {
        chromeDriver.close()
        chromeDriver.quit()
    }

Result: you will be using only one instance at time and if you see task manager you will not find chromedriver and chrome process hanging.

Bullnose answered 5/2, 2019 at 21:41 Comment(0)
H
0

You can use the Object Pool pattern for Web drivers in this scenario as below:
* * This class creates the pool of WebDriver instances as defined in the Main.java class by final variable "DRIVER_INSTANCES" and from that Main class we instantiate this pool

public class WebDriverPool {
    public static Vector<WebDriver> driverPools = new Vector<WebDriver>();
    
    public static void initializeWebDriverPool() {
        for(int i=0; i<Main.DRIVER_INSTANCES; i++) {
            System.setProperty("webdriver.chrome.driver", "chromedriver.exe");

            // Add options to Google Chrome. The window-size is important for responsive sites
            ChromeOptions options = new ChromeOptions();
            //options.addArguments("headless");
            options.addArguments("window-size=1200x600");
            WebDriver driver = new ChromeDriver(options);
            driverPools.add(driver);
        }
        System.out.println("Driver pool initialized");
    }
    
    public static WebDriver getAndRemove() {
        WebDriver driver = driverPools.get(0);
        driverPools.remove(0);
        return driver;
    }
    
    /*
     * When our all the task are finished then this method is called from the Main.java class to close the running chrome instances
     */
    public static void quitAllDrivers() {
        for(WebDriver driver: driverPools) {
            driver.quit();
        }
        
    }
}
Hassle answered 28/5, 2021 at 22:24 Comment(0)
T
0

For me, driver.quit() is working and killing all the process, no problem with that. But each time I use driver.close(), process remains. If I use it 10 times, 10 process stays on background process until I killed them programmatically or manually. Also driver.close() is giving some warnings at the end of running program most of the time like after closing browser

[WARNING]: Timed out connecting to Chrome, retrying...

Tick answered 5/2, 2023 at 10:54 Comment(0)
F
0

A very late addition. Set a breakpoint on driver.quit() after the driver.close().

If there are any WindowHandles left open on the driver object at this point they will leave residual chrome processes running in the background.

This is my implementation:

[AfterScenario]
public void Dispose()
{
    // Wrap in a try catch, once there are no more WindowHandles, trying to access the collection throws an exception
    try
    {
        while (_context.Driver.WindowHandles.Count > 0)
        {
            _context.Driver.SwitchTo().Window(_context.Driver.WindowHandles[0]);
            _context.Driver.Close();
        }
    }
    catch (Exception) { }

    _context.Driver.Quit();
}
Fishing answered 16/5 at 15:30 Comment(0)
W
-13

The problem was with driver.quit() method only for Chrome. The driver quit didnt work properly it didnt kill all processes of chrome (including child processes). What I did. I changed Selenium jar codes to fix this for my project , unfortunatly I cant share my code caus of project rules which dont allow to share any type of code .

Whiffet answered 24/11, 2017 at 19:0 Comment(1)
You're bound by IP agreements, sure. But, there is nothing wrong in explaining the logic, if you have one. That's not IP.Dx

© 2022 - 2024 — McMap. All rights reserved.