[英]Selenium Java: Guarantee chromedriver.exe shutdown when program exits
I'm using Selenium
in Java to automate Google Chrome
. 我在Java中使用Selenium
来自动化Google Chrome
。 When my program exits, a chromedriver.exe
instance is left in memory. 当我的程序退出时, chromedriver.exe
实例将保留在内存中。 I know that I can use driver.quit()
in eg a finally
block to shut down the current instance. 我知道我可以在例如finally
块中使用driver.quit()
来关闭当前实例。 However, if the Java
application is killed at any time (like during debug sessions), this cleanup is of course never performed. 但是,如果Java
应用程序在任何时候被杀死(例如在调试会话期间),则当然不会执行此清理。
I'm looking for a solution to make the chromedriver.exe
a child process of the Java
process so when the Java
process exits, the child process also dies automatically. 我正在寻找一种解决方案,使chromedriver.exe
成为Java
进程的子进程,因此当Java
进程退出时,子进程也会自动死亡。 Solutions like Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
解决方案如Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe");
kill all chromedriver.exe
instances but this may mess up other Selenium
processes on the system which is not ideal. 杀死所有chromedriver.exe
实例但这可能会chromedriver.exe
系统上其他不理想的Selenium
进程。 Also this would not work against forcibly killing the process either. 此外,这也不利于强行杀死该过程。
you can use "Selenium Grid" - it has a cleanup cycle of all idle sessions every XXX amount of time (default is 5 minutes) - https://www.seleniumhq.org/docs/07_selenium_grid.jsp 你可以使用“Selenium Grid” - 它每隔XXX个时间(默认为5分钟)有一个所有空闲会话的清理周期 - https://www.seleniumhq.org/docs/07_selenium_grid.jsp
This way, your selenium server will take care of calling driver.quit() [which is the preferred way for killing your processes - release Selenium chromedriver.exe from memory even if your Java program is killed. 这样,你的selenium服务器将负责调用driver.quit()[这是杀死你的进程的首选方法 - 即使你的Java程序被杀死, 也会从内存中释放Selenium chromedriver.exe 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.