Pages

How to Kill Existing Opened Browsers using selenium with java ?


Below program will close already opened browser instances and launch new brower instance.

import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

public class CloseExistingBrowsers{

public static void main(String[] args) {
try {
WebDriver driver;
String exePath = "C:\\IEDriverServer.exe";
System.setProperty("webdriver.ie.driver", exePath);
driver = new InternetExplorerDriver();
     // Pass which browser you want to close
CloseExistingBrowserInstances("FF");
driver.get("https://keeplearners.blogspot.com");
}catch (Exception e) {
e.printStackTrace();
}

}

public static void CloseExistingBrowserInstances(String browser) throws Exception {
if (browser.equalsIgnoreCase("IE")) {

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

} else if (browser.equalsIgnoreCase("chrome")) {

Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
} else {

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

Please comment below to feedback or ask questions.

No comments:

Post a Comment

Please comment below to feedback or ask questions.