nairavs
nairavs

Reputation: 509

Selenium webdriver does not quit chrome driver

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

Upvotes: 10

Views: 37953

Answers (9)

Reg Smith
Reg Smith

Reputation: 176

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();
}

Upvotes: 0

moto g
moto g

Reputation: 1

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...

Upvotes: 0

Lakshyaveer Chaudhary
Lakshyaveer Chaudhary

Reputation: 29

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();
        }
        
    }
}

Upvotes: 0

lucaswxp
lucaswxp

Reputation: 2119

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}')`)

Upvotes: 3

Santu Roy
Santu Roy

Reputation: 41

It works fine for me if I use

driver.close();
driver.quit();

Upvotes: 4

Dalvik_663
Dalvik_663

Reputation: 51

I solved them this way:

import os

os.system('killall chrome')

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

Upvotes: 4

sarath
sarath

Reputation: 459

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.

Upvotes: 1

nairavs
nairavs

Reputation: 509

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 .

Upvotes: -13

aholt
aholt

Reputation: 2971

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();
}

Upvotes: 5

Related Questions