- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 do I set browser width and height in Selenium WebDriver?
We can set browser width and height in Selenium webdriver. There are multiple methods available to achieve this. Whenever an application is launched, it opens in its default browser size.
We can resize the browser with the help of the Dimension class in Java. We create an object of the Dimension class and pass the desired width and height of the browser as parameters to that class. Finally we pass the object of the Dimension class as an argument to the setSize method.
Syntax
Dimension dem = new Dimension(750,450); driver.manage().window().setSize(dem);
We can also set the browser width and height with the help of Chrome options. We have to create an object of the ChromeOptions class and apply addArguments to it. The parameter window-size set with values of height and width of the browser are passed as arguments to the method. This knowledge is passed to the browser invocation method with the help of DesiredCapabilities class.
Syntax
ChromeOptions options = new ChromeOptions(); options.addArguments("window-size=750,450"); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(capabilities);
Example
Code Implementation with Dimension class.
import org.openqa.selenium.Dimension; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class BrowserResize{ 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/tutor_connect/index.php"; driver.get(url); // Dimension class with browser width and height value passed Dimension dem = new Dimension(750,450); // passing the Dimension object as an argument to setSize method driver.manage().window().setSize(dem);
Implementation with Chrome Options.
import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class BrowserResizeOptions{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); // ChromeOptions class with addArguments method ChromeOptions options = new ChromeOptions(); options.addArguments("window-size=750,450"); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(ChromeOptions.CAPABILITY, options); WebDriver driver = new ChromeDriver(capabilities); String url = "https://www.tutorialspoint.com/tutor_connect/index.php"; driver.get(url); } }
- Related Articles
- How do I set the Selenium webdriver get timeout?
- How do I open Chrome in selenium WebDriver?
- How do I resolve the ElementNotInteractableException in Selenium WebDriver?
- How to launch Edge browser with Selenium Webdriver?
- How do I get current URL in Selenium Webdriver 2 Python?
- How does selenium webdriver upload files to the browser?
- How to close child browser window in Selenium WebDriver using Java?
- How to maximize the browser window in Selenium WebDriver using C#?
- How do I get the width and height of a Tkinter window?
- How can I set an ImageView's width and height programmatically in Android?
- How to set viewport height and width in CSS
- How to set cell width and height in HTML?
- Clear browser Cookies with Selenium WebDriver Java bindings.
- Capturing browser logs with Selenium WebDriver using Java.
- How I can set the width and height of a JavaScript alert box?
