Selenium: how to prevent a just-started Chrome window from receiving focus



  • 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)



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2