title: Selenium ChromeDriver - Run in background but not headless
body: <p>I am looking for a way to run selenium tests with a chrome driver, in the background. With the background I mean as in, not the foreground focussed window. I can actually do this, but as soon as actions like opening a new tab or switching between windows (so basically switching window handles) happen in the chrome driver, the browser window gets pushed to the foreground.</p>
  
  <p>So my question is, how can I prevent this from happening without running the test headless?</p>
  
  <p>Any suggestions are appreciated, open for discussion.</p>
  
  <p><hr></p>
  
  <h2>EDIT</h2>
  
  <p>As a somewhat temporary solution I came up with the following.</p>
  
  <p>Using the Windows 10 Virtual Desktops feature, I run the test and thus the chrome browser window in a seperate virual desktop.
  I then switch back to my main virtual desktop to continue with other tasks.
  This prevents the chrome browser window from being forced to the foreground.</p>
  
  <p>Note that this still makes a flashing chrome icon appear in the taskbar when any of the actions described above appear.</p>
  
  <p>Still looking for a more solid solution, so any suggestions are still appreaciated.</p>
  
  <hr>  
  
  <p>To hopefully open new perspectives and discussion points, and ultimately a solution, I will provide some more detailed info of what my code is doing.  </p>
  
  <p>I have 5 chrome webdrivers, and each of these webdrivers contains 6 tabs (WindowHandles).
  The idea is that a certain process has to be repeated continuously on each tab. So we loop over each webdriver, and within that webdriver over each tab and set this tab as the webdrivers current WindowHandle. This makes the chrome window visibly switch to the assigned tab.</p>
  
  <p>After that switch has taken place, so basic selenium automation is performed on the content of the tab, after wich we repeat the whole process.</p>
  
  <p>The actual issue seems to take place when a chrome webdriver switches to a new tab (WindowHandle), at this moment the chrome window containing the tab is pushed to the foreground and steals focus. Note that this does not always takes place, often it can switch tabs without any issues. So it is unclear wether there is another factor which would cause the window to steal focus.</p>
  
  <p><hr></p>
  
  <h2>EDIT 2</h2>
  
  <p>After doing the following:  </p>
  
  <blockquote>
    <p>I overloaded the selenium method which is used to switch between tabs (WindowHandles), and called SetWindoPos each time. Unfortunately this did not solve this issue either. I will try to look deeper into what might be causing this and will report back. – S. Van den Wyngaert  </p>
  </blockquote>
  
  <p>I went out for a few hours while running the tests, and came back to see that surprisingly I was still on my main Virtual Desktop (win10 feature). This means that the issue didn't occur during the time I was gone. I started working again, opened a chrome window and noticed that shortly after I did this, focus was stolen by one of the chrome driver's windows again.</p>
  
  <p>What I conclude from this is that the issue only occurs when another chrome window (not opened by a chrome driver from code) is opened.
  I will keep investigating and will report back with updates.</p>
  
  <p>After investigating this behavior for a few more hours I noticed that when another chrome window is open, as long as this has focus, the issue doesn't take place.  So to quickly summarize this:</p>
  
  <p><strong>The issue doesn't take place when:</strong></p>
  
  <ul>
  <li>No other chrome windows (not selenium driven) are opened or minimised</li>
  <li>Another chrome window (not selenium driven) is opened and has focus</li>
  <li>Another application running fullscreen mode has focus</li>
  </ul>
  
  <p><strong>The issue does take place when:</strong></p>
  
  <ul>
  <li>Another chrome window (not selenium driven) is opened and has no focus</li>
  </ul>
  
  <p>Note that when I talk about another chrome window I specificly mean a chrome window that is not driven by selenium, so a regular chrome window opened by the user.</p>