Raclos
Raclos

Reputation: 85

Is there any way to minimize chrome window in Selenium?

Something like driver.manage().window().maximize(); but for minimize the window. Thanks!

Upvotes: 1

Views: 3723

Answers (4)

undetected Selenium
undetected Selenium

Reputation: 193098

Selenium's java client have no built-in method for minimizing the browser. Ideally, you shouldn't minimize the browser while the Test Execution is In Progress as Selenium would loose the focus over the Browsing Context and an exception will be raised at any point of time which will halt 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 solution:

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())));

Upvotes: 1

mk_
mk_

Reputation: 174

Use below code to completely minimize it.

driver.manage().window().setPosition(new Point(-2000, 0))

Upvotes: -1

Guy
Guy

Reputation: 50819

Selenium doesn't have minimize() option, atleast not for Java, however you can use setPosition do do it

driver.manage().window().setPosition(new Point(0, 0));

However the better way is to run it as headless browser

ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);

This way you can use maximized browser while it's running in the background.

Upvotes: 2

Indrajeet
Indrajeet

Reputation: 66

You can set the position of the WebDriver outside of your view. That way, it'll be out of sight while it runs.

FirefoxDriver driver = new FirefoxDriver();

driver.manage().window().setPosition(new Point(-2000, 0));

OR

Dimension windowMinSize = new Dimension(100,100); driver.manage().window().setSize(windowMinSize);

Upvotes: 0

Related Questions