2

I want to kill the browser instance (chrome) that was opened by web driver previously. How would I do that? In my code below, I intentionally didn't want to include quit() or close() as I want to leave the browser open. So every time I execute or run this program, I want to kill/close the previously opened browser and then start a new instance and leave it on. As a result, only one instance of browser should be open at a time. I am using Mac.

public static void main(String[] args){

    String website = "http://www.google.com";
    System.setProperty(".....");
    WebDriver driver = new ChromeDriver();
    driver.get(website);

}

The behaviour atm is that everytime I execute this, chrome instance will just pile up. What is the best way to avoid this? I am not doing this for testing purpose. I'm doing this because I want to automate a task. Thanks.

jumbo_siopao
  • 157
  • 1
  • 9
  • 1
    Perhaps driver.Quit() in tear down step after each test? Do kill java process! This is not a good practice. Just read quick selenium tutorial with examples. – Zhivko.Kostadinov May 04 '18 at 06:09
  • Possible duplicate of [Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?](https://stackoverflow.com/questions/47999568/selenium-how-to-stop-geckodriver-process-impacting-pc-memory-without-calling) – undetected Selenium May 04 '18 at 06:13

2 Answers2

0

You can try out this code :

public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        try{
            Runtime.getRuntime().exec("TASKKILL /F /IM chrome.exe");
        }
        catch(IOException io){
            System.out.println(io.getMessage());
        }
    }  

Note : It will kill all instances of chrome that was previously opened along with the newly opened instance.

cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

If I understand correctly, you are trying to automate a part of the flow, then let the automation program exit leaving the browser open for completing rest of the steps manually. In this case, the best way to ensure only one such window is open would be to keep the automation script idling until you are done with with manual task. Something like this at end of your main function:

try {
    while(true) { 
        Thread.sleep(1000); 
        driver.getCurrentUrl(); 
    } 
} catch(Exception e) {}

Would ensure that the program remains alive as long as the browser window is open. You can continue manual process in the browser. Once you are done, you can close the browser, which would automatically end this process. Or before starting the new task, you kill the old one with ^c, which in turn closes the browser.

The second option without keeping the automation script idling, would be to find the process id of newly created browser instance. You can save the process id in some file in temporary folder. Every time your script starts, it'd check the the file, read pid from it, and if the process id exists, try to kill it before spawning a new browser window.

d_shiv
  • 1,670
  • 10
  • 8