title: How to kill all browser windows and drivers before test execution on Windows
body: <p><strong>Resources : C# , Selenium 3.5 , NUnit , Windows , VS17</strong></p>
  
  <p>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. </p>
  
  <p>I know this command</p>
  
  <blockquote>
    <p>Taskkill /IM chrome.exe /F</p>
  </blockquote>
  
  <p>or </p>
  
  <blockquote>
    <p>Taskkill /IM chromedriver.exe /F</p>
  </blockquote>
  
  <p>but I  am not sure how to use/implement in Selenium - C#.</p>
  
  <p>The code below does not seem to be working if I run before init:</p>
  
  <pre><code>//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();
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>Please let me know if we have better solution to do this.</p>
  
  <p>I am using this until I get a good solution:</p>
  
  <pre><code>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();
      }
  }
  </code></pre>