4

Using Selenium + chromedriver on Linux, like so;

chrome_options = Options()
driver = webdriver.Chrome('/usr/bin/chromedriver',options=chrome_options)
driver.get('https://www.somewebsite.com')

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)

4
  • 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
    Commented Jun 13, 2019 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. Commented Jun 13, 2019 at 17:04
  • Open Chrome in headless mode? Commented Jun 13, 2019 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 Commented Jun 14, 2019 at 23:08

2 Answers 2

0

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.

3
  • 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) Commented Jun 15, 2019 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. Commented Jun 15, 2019 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
    Commented Jun 15, 2019 at 15:56
0

You can try adding the below chrome options here to enable to shift focus mode

System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--no-sandbox");
    options.addArguments("--disable-backgrounding-occluded-windows");
    options.addArguments("--disable-infobars");
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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