Make sure that browser opened by webdriver is always in focus

selenium, webdriver

Solution

`executeScript("window.focus();")` didn't work for me in the latest Chrome (v47 at the time of this post)

However I found a hack in another question which does work in this version of Chrome.

Here are the generic steps, since the question doesn't specify the Selenium API language:

- Show alert by executing script in browser

- Accept the alert

Implementation in `webdriverjs`, which I'm using

const chrome = setupChromeWebdriver(); // get your webdriver here

chrome.executeScript('alert("Focus window")'))
  .then(() => chrome.switchTo().alert().accept());

Problem

If the browser window is not in focus, all webdriver identifications on the current page fail. How can the browser be brought into focus, using webdriver?

Original source

Related problems