简体   繁体   English

在 Selenium / Java 中杀死 chromedriver 进程

[英]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).我目前正在使用“定期构建”选项通过 Jenkins 运行多个 Java 程序,并使用 H 06 * * 1-5(从周一到周五每天早上 6 点到早上 7 点运行它)。

There are certain programs in which i click on Links which opens up a new window. Hence, I use the below Code在某些程序中,我单击链接会打开一个新的 window。因此,我使用以下代码

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.现在程序运行后,由于 ChromeDriver.exe 进程已经在运行,我接下来的其他程序失败了。

I tried using driver.quit() instead of driver.close() in the code above, but it will close my entire browser.我尝试在上面的代码中使用driver.quit()而不是driver.close() ,但它会关闭我的整个浏览器。

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.注意:我在程序末尾使用driver.quit() ,这并不能帮助我摆脱在切换 window 时打开的正在运行的 Chromedriver.exe 实例。

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#.我一直在 JAVA 中寻找这个解决方案。但大多数时候我看到的是 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.直到那时我才发现进程下有很多 chromedriver.exe(和 FF 的 geckodriver.exe)实例。 I use this to kill my ChromeDriver.exe instance.我用它来杀死我的 ChromeDriver.exe 实例。

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:不要在特定页面上使用driver.close() ,让它在驱动程序工厂中使用以下代码:

//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.由于您正在单击打开新窗口的链接,因此最初我们必须存储父窗口的WindowHandle ,稍后我们将使用它来遍历回父窗口。 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.在新窗口打开后单击Terms of Use链接Terms of Use我们会将Selenium的焦点转移到新窗口,在那里执行我们的任务,最后关闭它。 Then we have to shift Selenium 's focus back to the parent window through the parent window handle as follows:然后我们必须通过父窗口句柄将Selenium的焦点移回父窗口,如下所示:

    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.由于我们无法看到您如何管理 WebDriver 会话的全貌,这只是一个猜谜游戏。

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.驱动程序本身可以通过命令行taskkill ,例如taskkill (或您使用的任何操作系统),可以通过例如https://github.com/zeroturnaround/zt-exec库执行。 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.finally块中使用taskkill ,如下所示。 It will kill the task chromedriver.exe :它将chromedriver.exe任务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 taskkill /FI "IMAGENAME eq chromedriver.exe" /F可以为你施展魔法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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