How to resize current browser window in Selenium WebDriver with Java?
Asked Answered
E

5

17

I need to resize the browser as 300x400 during executing the Selenium automated tests. How can I resize the browser window in Selenium WebDriver (aka, Selenium 2) with Java?

[Note: It needs to resize the browser window for testing Responsive Web Design (RWD)]

Everlasting answered 21/5, 2013 at 7:28 Comment(0)
L
32

You can get a reference to the current window with driver.manage().window(). And the window has a setSize() method, so you could try

Dimension dimension = new Dimension(800, 600);
driver.manage().window().setSize(dimension)
Linskey answered 21/5, 2013 at 7:36 Comment(0)
J
1

For Responsive Design it's better if you don't use fixed data, in this case screen resolution - users need most often (cause it's much less prone to errors) :

_driver.manage().window().maximize();

or

_driver.manage().window().fullScreen();
Joellajoelle answered 31/7, 2018 at 9:46 Comment(0)
B
1

If nothing works (to make tab in full screen only) for you, go for it:

driver.maximize_window()

where

driver = webdriver.Chrome(executable_path="D:\softwares\chromedriver.exe")

or could be any other webdriver locations.

Bibliophage answered 26/6, 2021 at 7:59 Comment(0)
G
0

We can also use Chrome capability to resize the window.

ChromeOptions options = new ChromeOptions();
options.addArguments("window-size=800,480");

DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
Grew answered 19/6, 2019 at 8:26 Comment(0)
G
0

After all comments above, I noticed that the suggestion below was not mentioned yet. It resolved the issue I had in my project. I hope it might solve your problem.

Java

driver.manage().window().fullscreen();
Germaun answered 11/8, 2023 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.