2

My question is very similar to this question. I launch many instances of WebDriver and some of them don't respond to driver.quit(). As described in this response I am able to kill browser instance by retrieving PID directly from capabilities object via the following code:

  • Code Block:

    import java.io.IOException;
    import org.openqa.selenium.Capabilities;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.remote.RemoteWebDriver;
    
    public class Kill_Firefox_PID {
    
        public static void main(String[] args) throws IOException {
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
            System.out.println("moz:processID value is : "+cap.getCapability("moz:processID"));
            Runtime.getRuntime().exec("taskkill /PID "+cap.getCapability("moz:processID"));
        }
    }
    
  • Console Output:

    moz:processID value is : 8492
    

While this works excellently to kill browser itself I still see instances of Geckodriver when I open task manager. As stated in the other thread I am unable to indiscriminately kill all Geckdriver instances via taskkill /f /im geckodriver.exe as there are some instances that are needed.

In short, I want to be able to retrieve PID of Geckodriver via capabilities and destroy those instances of Geckodriver (via PID) in a similar way that we are able to retrieve PID of Firefox WebDriver as outlined in response to this question.

Is this possible?

Thanks!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alan Cook
  • 342
  • 3
  • 14

1 Answers1

0

A new set of GeckoDriver and Firefox Browser are initiated when you invoke:

WebDriver driver = new FirefoxDriver();

You can find a detailed discussion in Is this correct - FirefoxDriver driver = new FirefoxDriver();?


The life span of GeckoDriver / Firefox Browser ceases to exist when you invoke:

driver.quit()

You can find a detailed discussion in PhantomJS web driver stays in memory


Solution

So a generic solution would be, instead of keeping the keeping/preserving the stale instances of invoke close() or quit() as per the flow of your test execution which will relieve you from indiscriminately killing all the GeckoDriver instances.

However at the end of your Test Execution if you want to kill all the GeckoDriver instances, you can use the following solution:

public class Killing_GeckoDriver_PID {

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

        String line;
        String pidInfo ="";
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        Process p =Runtime.getRuntime().exec(System.getenv("windir") +"\\system32\\"+"tasklist.exe");
        BufferedReader input =  new BufferedReader(new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            pidInfo+=line; 
        }
        input.close();

        if(pidInfo.contains("geckodriver.exe"))
        {
            Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe"); 
        }
        System.out.println("Killed all the instances of GeckoDriver");
    }
}

You can find a alternative approach within the discussion Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352