7

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

  • 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. – Niels van Reijmersdal Nov 4 '15 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. – kanayikkaran Nov 4 '15 at 12:30
4

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).
  • One more option might be to use 'HtmlUnitDriver' (on similar lines to PhantomJS). – JustARandomGuy Nov 5 '15 at 2:16
  • Yes, that too is there but I thought that OP may be knowing about that. – Dhiman Nov 5 '15 at 2:23
  • Thanks a lot for the answer. I'm gonna try PhantomJS and see how it works. – kanayikkaran Nov 5 '15 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 '18 at 23:21
0

You can also resize the window:

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

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

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

0

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

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

  • Can you add a reference link of documentation! – Nawshad Rehan Rasha Dec 5 '18 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 '18 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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