简体   繁体   中英

Kill chromedriver process in Selenium / Java

I am currently running multiple Java programs through Jenkins using "Build Periodically" option and uses H 06 * * 1-5 (run it every day between 6 AM and 7 AM from Monday to Friday).

There are certain programs in which i click on Links which opens up a new window. Hence, I use the below Code

driver.findElement(By.xpath(".//*[@id='terms']/li[1]/a")).click();
System.out.println("Home Page is loaded Successfully and Terms of Use Link is clicked");
ArrayList<String> window1 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(window1.get(1));
Thread.sleep(3000);
driver.close();
Thread.sleep(3000);
driver.switchTo().window(window1.get(0));

Now after the program runs, My other program following this fails because of the ChromeDriver.exe process that is already running.

I tried using driver.quit() instead of driver.close() in the code above, but it will close my entire browser.

Note: I have used driver.quit() at the end of my program which doesn't help me getting rid of the running Chromedriver.exe instance opened when i switched window.

Please suggest me aa good way to handle this issue. I have been looking for this solution in JAVA. But mostly i see answers for C#.

Thanks

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.

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 

@AfterClass
public void tearDown()
{
    driver.quit();
}

It should help.

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

The root cause could be in the other place. Such issues usually happen in case of unexpected exceptions thrown, which prevent your framework from quitting the session cleanly.

As we can't see the entire picture of how you're managing WebDriver sessions, it'll be just a guessing game.

Driver itself could be killed via command line, eg taskkill (or whatever OS you're using), which could be executed via eg https://github.com/zeroturnaround/zt-exec library. But it'd be just a workaround, which will hide a potential weakness of your architecture.

use taskkill in the finally block like shown below. It will kill the task chromedriver.exe :

finally {
    if(System.getProperty("os.name").contains("Windows")) {
            Process process = Runtime. getRuntime(). exec("taskkill /F /IM chromedriver.exe /T");
            process.destroy();
    }
}

taskkill /FI "IMAGENAME eq chromedriver.exe" /F could do the magic for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM