Set Specific Window Width
To set specific window width to the browser using Selenium for Java, call driver.manage().window().setSize()
method and pass the Dimension object as argument. The Dimension object can be initialized with required width and height. Width is set to the required value, and height is set to the existing height value.
The following is a simple code snippet to set the window size to a width of 800 and keeping the original height.
</>
Copy
int width = 800;
int height = driver.manage().window().getSize().height;
driver.manage().window().setSize(new Dimension(width, height));
Example
In the following program, we write Selenium Java code to set the window size to a width of 800.
Java Program
</>
Copy
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyAppTest {
public static void main(String[] args) {
//initialize web driver
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
WebDriver driver = new ChromeDriver();
try {
//get the URL
driver.get("https://www.tutorialkart.com/");
//set new dimension to browser window
int width = 800;
int height = driver.manage().window().getSize().height;
driver.manage().window().setSize(new Dimension(width, height));
} finally {
driver.quit();
}
}
}
Screenshots
1. Screenshot before resize.

2. Screenshot after resize.
