Multiple Instances of Firefox during Selenium Webdriver Testing not handling focus correctly. Multiple Instances of Firefox during Selenium Webdriver Testing not handling focus correctly. selenium selenium

Multiple Instances of Firefox during Selenium Webdriver Testing not handling focus correctly.


@djangofan: Wrong. You cannot lock the focus. After you requested focus in one window and before you trigger an action, another window requests focus, and your action (like sending keys to input field) just doesn't work. This happened in our tests several times daily. It was hard to reproduce, because with each test run it failed on different places. A solution is to execute each browser in a separate display. E.g. you can use Xvfb:

  Xvfb ... -screen 1 1200x800x24 -screen 2 1200x800x24 ...

Then when you start a browser, assign a separate screen to it:

  browser.setEnvironmentProperty("DISPLAY", ":N.1");  browser.setEnvironmentProperty("DISPLAY", ":N.2");  ...


I've had the same issue in my continuous integration environment with Jenkins.After a long research i found an old bug in firefox that led to a new config flag to avoid those problems.

The solution is to enable this flag on the firefox profile that the tests use. The flag is focusmanager.testmode, set it to true.

enter image description here

The explanation is that the focus events are triggered only when firefox window is active. If you run multiple test you have multiple windows so only the active one triggers the focus events. With this param the events are trigered even for non active windows.


You can wrangle this and get it under your control with no problem. First write a method to identify the popup window by its window handle id. Then, use a JavaScriptExecutor to execute "window.focus()" in javascript to force the window to be focused just before you perform another action. Then, you can close the popup by its window handle name if necessary.