- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to set the size of the browser window in Selenium?
We can set the size of the browser window by the following methods −
getSize() method
Javascript executor
Example
With setSize() method.
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import java.util.List; public class BrowserDimension { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // maximize the browser driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); // fetching the current window size with getSize() System.out.println(driver.manage().window().getSize()); //Create object of Dimensions class Dimension dm = new Dimension(450,630); //Setting the current window to that dimension driver.manage().window().setSize(dm); driver.close(); } }
Example
With Javascript executor.
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.JavascriptExecutor; public class BrowserDimensionJS { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = "https://www.tutorialspoint.com/index.htm"; driver.get(url); // maximize the browser driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS); JavascriptExecutor js = (JavascriptExecutor) driver; // set size with window.resizeTo() method js.executeScript("window.resizeTo(450,630);"); driver.close(); } }
- Related Articles
- How to maximize the browser window in Selenium WebDriver using C#?
- How to set window size using phantomjs and selenium webdriver in Python?
- How to avoid the pop-up window in chrome browser with Selenium?
- How to close child browser window in Selenium WebDriver using Java?
- Get the size of the screen, current web page and browser window in JavaScript
- How to open browser window in incognito/private mode using python selenium webdriver?
- How to maximize the browser in Selenium?
- How to open a browser window in full screen using Selenium WebDriver with C#?
- How to set a Tkinter window to a constant size?
- How to open a new window on a browser using Selenium WebDriver for python?
- How to Resize Browser Window in WebDriver?
- How to Set a Tkinter Window with a Constant Size?
- How to find the size of the plotting window in R?
- How do I set a minimum window size in Tkinter?
- How do I set browser width and height in Selenium WebDriver?

Advertisements