-1

Resources : C# , Selenium 3.5 , NUnit , Windows , VS17

I want to kill all previous browser (let's take chrome.exe for now) windows as well as chromedriver.exe before I kick off test execution.

I know this command

Taskkill /IM chrome.exe /F

or

Taskkill /IM chromedriver.exe /F

but I am not sure how to use/implement in Selenium - C#.

The code below does not seem to be working if I run before init:

//we can give any process name to handle this
Process[] chromeDriverProcesses = Process.GetProcessesByName("chromedriver");
foreach (var chromeDriverProcess in chromeDriverProcesses)
{
    // that's how we can kill all chromedriver 
    chromeDriverProcess.Kill();
}

The reason I want to do this clean-up is because I want to run this with Jenkins and other VMs and we are seeing some issues. Issue can be resolved if we kill/ clear browser/driver before execution.

Please let me know if we have better solution to do this.

I am using this until I get a good solution:

if (Settings.BrowserType.Equals(Constant.Chrome))
{
    // check chrome driver  browser and kill
    Process[] chromeAndDriverProcesses = Process.GetProcessesByName("chromedriver");
    foreach (var chromeDriverProcess in chromeAndDriverProcesses)
    {
        chromeDriverProcess.Kill();
    }
    // check chrome browser and kill 
    chromeAndDriverProcesses = Process.GetProcessesByName("chrome");
    foreach (var chromeDriverProcess in chromeAndDriverProcesses)
    {
        //chromeDriverProcess.Kill();
    }
}
KilloWatt
  • 93
  • 1
  • 8
Mike ASP
  • 2,013
  • 2
  • 17
  • 24

3 Answers3

2

This should be working as far as I tried

System.Diagnostics.ProcessStartInfo p;
p = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/C "+ "taskkill /f /im chromedriver.exe");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();
proc.Close();

Refer here for more details

Harshavardhan Konakanchi
  • 4,238
  • 6
  • 36
  • 54
1

Try this:

foreach(Process P in Process.GetProcessesByName("chromedriver"))
    P.Kill();
foreach(Process P in Process.GetProcessesByName("chrome"))
    P.Kill();

Can you clarify what you mean by

this below seems not working if I run before init :

Wolf
  • 52
  • 3
  • 1
    You can read broken english better than I can. Nowhere in that post does it say he tried that. It's in the comments from what looks like a copy-pasted example, which does not guarantee it was uncommented and attempted. – Wolf Dec 26 '17 at 22:58
  • It's right there in the code he put in the post. Yours is just written differently than what is posted. Just saying, not trying to start a war here. – IamBatman Dec 26 '17 at 23:05
  • @Wolf, thanks for your comment. But I already tried your solution. Sorry for my broken question. – Mike ASP Dec 26 '17 at 23:12
0

The question I want to kill all previous browser shouldn't arise at all when following the Best Practices with Selenium.

While automating through Selenium as per the best practices you should invoke the quit() method within the tearDown() {}. Invoking quit() DELETEs the current browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint. Here is an example below :

1503397488598   webdriver::server   DEBUG   -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74 
1503397488607   geckodriver::marionette TRACE   -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821   webdriver::server   DEBUG   -> GET /shutdown

So on invoking quit() method the Web Browser session and the WebDriver instance gets killed completely. hence you don't have to incorporate additional Taskkill commands which looks to be an overhead.


Answer to this question :

Still if you want to execute Taskkill shell command to kill the Chrome Web Browser session and the ChromeDriver you can use the following code blocks :

  • Kill the Chrome Web Browser :

    string str_shell_command_chrome;
    str_shell_command_chrome = "taskkill /F /IM chrome.exe /T";
    System.Diagnostics.Process.Start("CMD.exe",str_shell_command_chrome);
    
  • Kill the ChromeDriver :

    string str_shell_command_chromedriver;
    str_shell_command_chromedriver = "taskkill /F /IM chromedriver.exe /T";
    System.Diagnostics.Process.Start("CMD.exe",str_shell_command_chromedriver);
    

Trivia

To hide the command prompt while killing the ChromeDriver binary, you can use the following block of code :

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "taskkill /F /IM chromedriver.exe /T";
process.StartInfo = startInfo;
process.Start();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • thanks for your reply. I was not looking Quit, Close, Dispose methods. My questions is when you are copying automation on different machine which could have some Chrome.exe/Chromedriver.exe (some possibility) open then how you are going to shot it down ? We don't want to run automation if there any single Chrome.exe/Chromedriver.exe exist. First kill off them and then start execution. – Mike ASP Dec 27 '17 at 17:38
  • @MikeASP Actually my Answer does contains a section to your individual query. Now I have made a separate section. Let me know if this answers your question. – undetected Selenium Dec 27 '17 at 17:54
  • 1
    It's not working. It can open CMD but can not provide 'taskkill' command there. string str_shell_command_chromedriver; str_shell_command_chromedriver = "taskkill /F /IM chromedriver.exe /T"; System.Diagnostics.Process.Start("CMD.exe",str_shell_command_chromedriver); – Mike ASP Dec 27 '17 at 23:13