How could I start a Selenium browser (like Firefox) minimized? I want the command browser.start(mySettings)
to start a browser minimized.
-
http://stackoverflow.com/questions/3224518/how-to-close-or-minimize-browser-windows-with-selenium-rc – Dave Patterson Nov 27 '11 at 17:17
-
Headless browsers are used for this purpose. – Chandra Shekhar May 05 '17 at 07:34
14 Answers
I have an alternate solution that may meet your needs. You can set the position of the WebDriver to be outside of your view. That way, it'll be out of sight while it runs. (It may start at a visible location, but it will only be there for an instant.)
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-2000, 0));

- 5,853
- 1
- 27
- 27
-
This won't work if your window manager prevents the window to be placed off-screen (which appears to be the case with awesome WM). Where does it work for you? – blueyed Jul 09 '14 at 15:31
-
I've never messed around with a window manager. I assume that means I'm using Selenium's default window manager. – NoBrainer Jul 09 '14 at 19:24
-
awesome WM is a desktop window manager, and I am using it on Linux. Are you using MacOS or Windows? – blueyed Jul 10 '14 at 00:26
-
Your question does not say that why you want to run your test cases in minimized browser but unfortunately selenium do not provide any built-in function for the same.
Normally when we want to run test cases with maximized browser we use driver.manage().window().maximize();
No doubt there are several ways to minimize your window through code by using Java key event by using keyboard shortcuts for minimimzing window or by using JavaScriptExecuter but that too depend on which OS and language you are working.
One more thing you can try is HtmlUnitDriver.By using this you cant even see the browser, so that may also serve your purpose if you have a case of not opening the browser while execution of test cases.

- 4,551
- 4
- 25
- 38
Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);

- 1,115
- 5
- 17
- 25
For C# you can minimize window easily, also with a built in way.
See: https://www.selenium.dev/documentation/en/webdriver/browser_manipulation
Screenshot:

- 31
- 4
-
1I'm also using C# only but from that link it appears that it should work easily for Java, Python, Ruby, JS, and Kotlin too. – jsabrooke Jun 09 '20 at 17:07
-
1
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.

- 54
- 3
Without knowing your motive for minimizing the browser and assuming that you are using the WebDriver drivers (Selenium v2) and don't want a UI to pop up, one could use the lightweight browser HtmlUnitDriver.

- 3,098
- 5
- 35
- 52
-
HTMLUnitDriver works well in general web pages but web pages coded in AJAX or heavy javascript sometimes performs very poorly! – roosevelt Nov 06 '13 at 21:00
When Using Python to Move the FireFox Browser off screen:
driver = webdriver.FireFox()
driver.set_window_position(-2000,0)

- 224
- 1
- 3
- 16
driver.set_window_position(2000,2000)
or
(x,y) values more than monitor resolution
Later if u want to see the window again
driver.maximize_window()

- 183,867
- 41
- 278
- 352

- 149
- 1
- 4
In php we can use JavaScript command to minimize the browser window.
$this->selenium->getEval("Minimize();");
and similar command for java :
browser.getEval("Minimize();");

- 183,867
- 41
- 278
- 352

- 99
- 2
-
1Doesn't this require a global `Minimize` function in the browser (which Firefox does not appear to have).? – blueyed Jul 09 '14 at 18:24
-
Seems that this does not work on Firefox indeed. ((IJavaScriptExecutor)driver).ExecuteScript("Minimize();"); – LeoPucciBr May 14 '17 at 14:43
The workarounds mentioned in the post did not work for NodeWebKit browser, so as a workaround i had to use native C# code as mentioned below:
public static void MinimiseNWKBrowser(IWebDriver d)
{
var body = UICommon.GetElement(By.TagName("body"), d);
body.Click();
string alt = "%";
string space = " ";
string down = "{DOWN}";
string enter = "{ENTER}";
SendKeys.SendWait(alt + space);
for(var i = 1; i <= 5; i++)
{
SendKeys.SendWait(down);
}
SendKeys.SendWait(enter);
}
So this workaround basically uses "ALT+SPACE" to bring up the browser action menu to select "MINIMIZE" from the options and presses "ENTER"

- 183,867
- 41
- 278
- 352

- 51
- 3
The best way to minimize your browser is to use shortcut using Robot class.
Shortcut: Alt+Space+N (for Windows)
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);
By using above code you can minimize your browser.

- 183,867
- 41
- 278
- 352

- 359
- 3
- 7
- 17
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())));

- 183,867
- 41
- 278
- 352
You can use:
driver.manage().window().maximize();
For example code snippet with chrome driver:
System.setProperty("webdriver.chrome.driver", "C://chromedriver.exe");
driver = new ChromeDriver();
baseUrl = "chrome://newtab/";
driver.manage().window().maximize().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
for Firefox use just "firefordriver.exe"

- 183,867
- 41
- 278
- 352

- 534
- 5
- 17
driver.manage().window().minimize();
This should help minimize the window. You can also use "maximize" in place of "minimize" to maximize the window.

- 19,814
- 17
- 56
- 77

- 27
- 2
-
6Which Selenium API is this? There appears to be only a method `maximize`, but nothing for minimizing (Selenium 2.42.1, Python API). – blueyed Jul 09 '14 at 18:31
-
I don't find any method .minimize(), selenium supports only .maximize(); – Pankaj Kumar Katiyar Jan 30 '15 at 05:50
-
It exists now in Selenium 4+. See https://www.selenium.dev/documentation/en/webdriver/browser_manipulation . I won't upvote though as it was presumably incorrect at the time... – jsabrooke Jun 09 '20 at 17:05