11

How can I minimize the browser window which has been launched via Selenium Webdriver?

I see that there is a method to maximize (documentation), but I cannot find the converse for minimize.

I have tried resizing and repositioning the window, however this does not work the same way as minimize.

Is there a way to do this in Selenium? Or can this only be done through Robot key events (e.g. Windows + D) ?

2
  • Just curious why do you want to minimize? Do you want to test something works while minimized? OR? In the past I have had issues with not full-screen browsers. Leading to start each test with maximizing the browser at the start. Nov 4, 2015 at 11:57
  • I don't really want to test anything while it is minimized. I was wondering why WebDriver have a maximize option, but not minimize. Also, I was recently asked this question in one of the interviews I attended. Nov 4, 2015 at 12:30

6 Answers 6

5

Short answer to your question: Unfortunately, Selenium do not provide any built-in function for minimizing the browser window, there is only function for maximizing the window. But there are some work around for doing this.

  1. You want window to be out of sight, then you need to use this code. by using it browser window will be out of your sight and you will not be able to see what is running, but don't worry your test cases will keep on executing. It is tried and tested work around.

Reference Link

driver.manage().window().setPosition(new Point(-2000, 0))
  1. You can use PhantomJS for running your test cases, download and install it from this link and simply change the line

driver = webdriver.Firefox() to driver = webdriver.PhantomJS()

and you don't need to change any more code. Your browser will run in an hidden mode.

Reference link

  1. Use the Robot class and windows shortcut keys for the same (as already mentioned by you).
4
  • One more option might be to use 'HtmlUnitDriver' (on similar lines to PhantomJS). Nov 5, 2015 at 2:16
  • Yes, that too is there but I thought that OP may be knowing about that.
    – Dhiman
    Nov 5, 2015 at 2:23
  • Thanks a lot for the answer. I'm gonna try PhantomJS and see how it works. Nov 5, 2015 at 5:20
  • why is everyone saying that? It's not true that phandomJS works with existing code and you don' t have to change anything.
    – veritaS
    Nov 3, 2018 at 23:21
2

Perhaps, I have the wrong context here (the OP's documentation link is broken now), but I believe you can now minimize the window with webdriver.minimize_window().

Python Documentation

2

For C# language:

driver.Manage().Window.Minimize();

This works fine with Chrome, Firefox, IE but fails for 'Edge' driver.

2
  • Can you add a reference link of documentation! Dec 5, 2018 at 5:19
  • @NawshadRehanRasha I couldn't find a proper documentation. It worked for me (in C#). I have some pictures of the code that I tried. Please check the link: github.com/priyanka-kondapuram/C-Sharp/issues/1
    – Priyanka
    Dec 20, 2018 at 18:50
1

The best I'd say is to type shortcut using Robot Class. Shortcut to minimize: Alt+Space+N

    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 the above code you can minimize your browser.

0

You can also resize the window:

 Dimension n = new Dimension(360,592);  
 driver.manage().window().setSize(n);
0
driver.manage().window().setSize(new Dimension(1200,800));

This will works for you.

1
  • 1
    Can you elaborate why this is different than sameer's answer from Nov 19, 2015?
    – corsiKa
    Mar 1, 2020 at 16:39

Your Answer

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

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