Maximize Browser in selenium

Most of the time, when you open the firefox browser with selenium, the browser will be in minimized mode, but in a few scenarios, we might want to maximize the window. we can maximize the browser window with the help of maximize() method present in the window class

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Quit
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://selenium-mentor.com");
		// below line maximizes the browser window
		driver.manage().window().maximize();
	}
}

Elaborated Code :
In the above program, we were able to maximize in one single line, but let's understand the code in an elaborated way

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.Window;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Quit
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");

		// below three line of code is used to maximize the browser
		Options man = driver.manage();
		Window win = man.window();
		win.maximize();
	}
}

We do not have any method to minimize the window of the browser, but we can resize the window.

Multi-level Inheritance in Java and Selenium

Browser Size in selenium

We can get the size of the browser window by using the getSize() method present in the Dimension class, and it returns the Dimension Class type value.

Code Example for getting the size of Window

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetSize
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://chercher.tech");
		// below lin will fetches the size of the window.
		Dimension size = driver.manage().window().getSize();
		System.out.println("The size of the window : "+size);
	}
}

The Output of the program:
The size of the window : (1024, 546)

If you don't want value with x and y instead of that, if you need to get height or width, then use the following way.

Code Example for getting the size of Window

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GetSize
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://selenium-mentor.com");
		int height = driver.manage().window().getSize().height;
		int width =driver.manage().window().getSize().width;
		int height1 = driver.manage().window().getSize().getHeight();
		int width2 = driver.manage().window().getSize().getWidth();
		System.out.println("The height of the window by height variable : "+height);
		System.out.println("The height of the window by width variable : "+height1);
		System.out.println("by methods");
		System.out.println("The height of the window by getHeight() variable : "+width);
		System.out.println("The height of the window by getWidth() variable : "+width2);
	}
}

The output of the above program:

The height of the window by height variable : 546
The height of the window by width variable : 546
by methods
The height of the window by getHeight() variable :
The height of the window by getWidth() variable : 1024
Optimized Code for getting the size of Window
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SetPosition
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://selenium-qtp-mentor.com");
		Dimension size = driver.manage().window().getSize();
		int height = size.height;
		int width =size.width;
		int height1 = size.getHeight();
		int width2 = size.getWidth();
		System.out.println("The height of the window by height variable : "+height);
		System.out.println("The height of the window by height1 variable : "+height1);
		System.out.println("by methods");
		System.out.println("The width of the window by getWidth() variable : "+width);
		System.out.println("The width of the window by getWidth() variable : "+width2);
	}
}

Open Browsers in Selenium

Set browser Window Size with selenium

We can set the size of the browser window by using the setSize() method present in the window class. It accepts the point class constructor as the argument.

import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SetSize
{
	public static void main(String[] args) throws Exception
	{
		WebDriver driver=new FirefoxDriver();
		driver.get("https://selenium-mentor.com");
		// below line sets the size of the browser
		driver.manage().window().setSize(new Dimension(200, 200));
	}
}

Add a screenshot to pdf in selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions