Using Selenium + chromedriver on Linux, like so;

chrome_options = Options()
driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)

Chrome will start and will receive immediate focus (most of the time, sometimes it does not).

One can easily click outside of the Chrome window and continue working elsewhere, it will not affect Selenium/Chromedriver at all.

However, if one was typing something then the text will now be entered in the just-focused browser window. When starting many windows in sequence, this is cumbersome (keep loosing focus).

How to avoid the just-started Chrome window from receiving focus in the first place?

(Coding language used is Python)

  • 1
    Seems like a bad approach in general - even if you solve this, once a test opens a new tab - it will focus on it. Instead run the test somewhere else (CI maybe?) or run them in headless mode. – Moro Jun 13 '19 at 11:29
  • Kind of agree with Moro, but, can't you just keep track of the window handles? Store the window handle you want, switch to it before you try some typing interaction. – SlightlyKosumi Jun 13 '19 at 17:04
  • Open Chrome in headless mode? – Bill Hileman Jun 13 '19 at 17:06
  • I would like to keep track of the progress for each window opened. Sometimes something minor fails and I need to fix it manually. It would also be great to run it on a machine that is otherwise used as well (as per the original question). Thank you – Roel Van de Paar Jun 14 '19 at 23:08

There are multiple workarounds you can try:

  1. Use Headless browser or run execution in hidden mode using "phantomjs". It is driver like chrome driver which start your execution in hidden mode. Phantomjs WebSite
  2. Minimize the browser using set position in selenium. Try below code for the same: driver.manage().window().setPosition(new Point(0, -1000));

I hope it will solve your problem.

  • Thank you! Headless is not an option (ref above). For the setPosition, I get; File "./do.py", line 89 driver.manage().window().setPosition(new Point(0, -1000)) SyntaxError: invalid syntax with the error pointer pointing to the t of Point (Python) – Roel Van de Paar Jun 15 '19 at 0:59
  • Got it to "work" with driver.set_window_position(0,-1000) and also tried driver.set_window_position(-2000,-2000) but both just move the window to position 0,0 on the screen (not minimized) and the focus is still there. – Roel Van de Paar Jun 15 '19 at 1:04
  • If you are using windows OS then you can try Selenium Action class to perform ALT+TAB to remove focus. And if that will not work then you can use any external library to perform keyword actions. Like in Java I tried below code and it perform ALT+TAB; Robot r = new Robot(); r.keyPress(KeyEvent.VK_ALT); r.keyPress(KeyEvent.VK_TAB); r.keyRelease(KeyEvent.VK_ALT); r.keyRelease(KeyEvent.VK_TAB); – Saurabh Jun 15 '19 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.