How could I start a Selenium browser(like Firefox) minimized?

Viewed 43917

How could I start a Selenium browser (like Firefox) minimized? I want the command browser.start(mySettings) to start a browser minimized.

14 Answers

After you define the driver

driver = webdriver.Chrome(r"FULL PATH TO YOUR CHROMEDRIVER")

Do

driver.minimize_window()

And then call the site

driver.get(r'SITE YOU WANT TO SELECT')

It will minimize.

Selenium doesn't have a built-in method to minimize the Browsing Context. Minimizing the browser while the Test Execution is In Progress would be against the best practices as Selenium may loose the focus over the Browsing Context and an exception may raise during the Test Execution. You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized


However, to mimic the functionality of minimizing the Browsing Context you can use the following steps:

  • Set the dimension of the Browsing Context to [0, 0]
  • Set the position of the top left corner of the Browsing Context just below the Viewport
  • Code Block (Java):

    driver.navigate().to("https://www.google.com/");
    Point p = driver.manage().window().getPosition();
    Dimension d = driver.manage().window().getSize();
    driver.manage().window().setSize(new Dimension(0,0));
    driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
    
Related