Kill chromedriver process in Selenium / Java Kill chromedriver process in Selenium / Java jenkins jenkins

Kill chromedriver process in Selenium / Java


I'm fairly new and still learning. I noticed quite recently my available memory was running low. Only then I found out that there were lots of instances of chromedriver.exe (and geckodriver.exe for FF) under processes. I use this to kill my ChromeDriver.exe instance.

Runtime.getRuntime().exec("taskkill /F /IM ChromeDriver.exe");

It works just fine.


Since you are clicking on a link which opens a new window, initially we have to store the WindowHandle of the parent window which we will use later to traverse back to the parent window. On clicking on the link Terms of Use once the new window opens we will shift Selenium's focus to the new window, perform our tasks there and finally close it. Then we have to shift Selenium's focus back to the parent window through the parent window handle as follows:

    driver.navigate().to("someurl.com");    String parent = driver.getWindowHandle();    System.out.println("Parent Window ID is : "+parent);    driver.findElement(By.xpath("Link_of_Terms_of_Use_opens_New_Window")).click();    System.out.println("Home Page is loaded Successfully and Terms of Use Link is clicked");    Set<String> allWindows = driver.getWindowHandles();    int count = allWindows.size();    System.out.println("Total Windows : "+count);    for(String child:allWindows)    {        if(!parent.equalsIgnoreCase(child))        {            driver.switchTo().window(child);            System.out.println("Child Window Title is: "+driver.getTitle());            //perform all your tasks in the new window here            driver.close();        }    }    driver.switchTo().window(parent);    System.out.println("Parent Window Title is: "+driver.getTitle());


Don't use driver.close() on the particular page, let it be and use the code below in driver factory:

//This closes the browser after each test class @AfterClasspublic void tearDown(){    driver.quit();}

It should help.