Java 如何杀死在后台运行的 IEDriver exe 进程(Selenium webdriver)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22652692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:07:17  来源:igfitidea点击:

How to Kill IEDriver exe process running in background (Selenium webdriver)?

javainternet-explorerselenium-webdrivertaskmanager

提问by user1254261

I'm using selenium webdriver(for Internet Explorer). What it does it basically opens a webpage in internet explorer and does form submitting.

我正在使用 selenium webdriver(用于 Internet Explorer)。它所做的基本上是在 Internet Explorer 中打开一个网页并提交表单。

How can I kill internetexplorer.exe running in background Automatically ?

如何自动终止在后台运行的 internetexplorer.exe?

回答by Sitam Jana

Close browser:

关闭浏览器:

try{
WebDriver driver = new InternetExplorerDriver();
.. write all the webdriver code here like driver.get, driver.findElement().click() etc. etc.
}
catch(Throwable webDriverException){
  if(webDriverException.getMessage().contains("org.openqa.selenium.WebDriverException: Error communicating with the remote browser. It may have died"){
      // Kill IEDriverServer.exe process
      // Using WebDriver WindowUtils utility 
      WindowsUtils.killByName("IEDriverServer.exe");

      // Or using JavaRunTime
     Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe")
  }

}

See if this helps!!!

看看有没有帮助!!!

回答by Sighil

You can add the following code at the end of your test script to close the IE Driver. So there is no need of closing it manually.

您可以在测试脚本的末尾添加以下代码来关闭 IE 驱动程序。所以不需要手动关闭它。

try {
    Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe");
} catch (IOException e) {
    e.printStackTrace();
}

Else open notepad and paste the following code.

否则打开记事本并粘贴以下代码。

taskkill /F /IM IEDriverServer.exe

Save the file as closedriver.bat

将文件另存为 closedriver.bat

Click on this batch file when u want to close the IE Driver.

当您要关闭 IE 驱动程序时,请单击此批处理文件。

回答by AL Lopez

If you are using MS test, on [TestCleanup]or [ClassCleanup]add the following:

如果您使用的是 MS 测试,请在[TestCleanup][ClassCleanup] 上添加以下内容:

foreach(var process in Process.GetProcess("IEDriverServer"))
{
  process.Kill();
}