How to set window size for Chrome in Selenium?
Asked Answered
D

18

88

I'm using Selenium WebDriver for automation and I'm using Chromedriver.

I have noticed that when my driver runs and opens the chrome browser, it opens the browser with a strange size. I tried to fixed it but in vain.

Does anybody know how can I change it?

Dejected answered 30/4, 2014 at 6:43 Comment(1)
What language binding are you using? The API provides methods to set windows size regardless which driver you are using (unless you have specific issues with ChromeDriver, then please provide more information in that case). Here's an article you might want to have a look. How to get window size, resize or maximize window using Selenium WebDriverEdmundedmunda
G
128

Python

Drivers

chrome = 57.0.2987.133
chromedriver = 2.27.440174

Code:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--window-size=1920,1080")
driver = Chrome(chrome_options=chrome_options)
Goldarn answered 3/4, 2017 at 9:4 Comment(8)
Yes, latest window resize is buggy in chromedriver, they say its fixed in 2.28 version , but I got error in that version also . I also end up using command line switches and passing window size as args.Croydon
Thanks! Using ChromeDriver 2.32 and this is the only option that works for a headless variation. C# Windows. driver.Manage().Window.Size does not work.Bugaboo
I notice that it seems to have a maximum size that it silently reverts to if you go over. (My machine, 1940 pixels high, but it seems to differ.)Cristophercristy
Update: My comment is true for Chrome, but not Firefox.Cristophercristy
this notation also works chrome_options.add_argument('--window-size=768x1024')Reconstruct
@Reconstruct that format didn't work for me using Selenium .NET, while the format in the answer did workCamaraderie
chrome_options is now deprecated in favour of "options", so it should be driver = webdriver.Chrome(options=chrome_options)Tinsmith
This got me 90% of the way towards solving my issue (trying to get a 3840x2160 screenshot of a Chrome webpage using Selenium). However, in order for the screenshot to get rendered as a 3840x2160 file, I also had to add options.add_argument('--headless'). Otherwise, the screenshot's dimensions would max out at 3,848 by 1,868 pixels (even when adding options.add_argument('--window-size=3840,2160')).Induline
I
37

Use this for your custom size:

driver.manage().window().setSize(new Dimension(1024,768));

you can change your dimensions as per your requirements.

Ils answered 30/4, 2014 at 7:48 Comment(3)
Doesn't seems to work for headless Chrome (version 60). However, @Yonatan Kiron's answer does worksBelaud
@MiguelD'Alessio Glad that it helped :)Ils
Puppeteer solution: see my answer on another thread to do it in using session.send('Browser.setWindowBounds', {windowId, bounds: {width: width, height: height}}) in a Chrome Devtools Protocol (CDP) session.Marxism
B
18
#use chrome webdriver

driver = webdriver.Chrome('path to /chromedriver')
driver.set_window_size(1400,1000)
Birk answered 16/6, 2016 at 13:39 Comment(3)
this is link to webdriver chrome chromedriver.storage.googleapis.com/index.html?path=2.22Birk
this one worked for me. Driver Version 81.0.4044.138Dissociable
Also a Winner. Thanks dudeImpracticable
P
12

C# version of @yonatan-kiron's answer, and Selenium's using statement from their example code.

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--window-size=1300,1000");

using (IWebDriver driver = new ChromeDriver(chromeOptions))
{
    ...
}
Pikestaff answered 15/12, 2017 at 2:59 Comment(1)
All the other answers but this are non-default window size, meaning it will still launch ugly +1.Orthopter
I
10

try this

using System.Drawing;
driver.Manage().Window.Size = new Size(width, height);
Illtimed answered 30/4, 2014 at 7:37 Comment(0)
R
5

Try with driver.manage.window.maximize(); to maximize window.

Rapping answered 30/4, 2014 at 6:48 Comment(1)
Thanks, but I don't want a full screen, but to fix the window's size by my own size. The Chromedriver for some reason fix the window size when open the browser.Dejected
P
4

RUBY


Approach #1

options = {
      'chromeOptions' => {
          'args' => ['start-fullscreen']
      }
  }

caps = Selenium::WebDriver::Remote::Capabilities.chrome(options)
@driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps

Approach #2

options = {
      'chromeOptions' => {
          'args' => ['window-size=640,480']
      }
  }

caps = Selenium::WebDriver::Remote::Capabilities.chrome(options)
@driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps

Approach #3

max_width, max_height = @driver.execute_script("return [window.screen.availWidth, window.screen.availHeight];")
@driver.manage.window.resize_to(max_width, max_height)

Approach #4

@driver.manage.window.maximize

Approach #5

target_size = Selenium::WebDriver::Dimension.new(1600, 1268)
@driver.manage.window.size = target_size

Approach #6

@driver.manage.window.resize_to(640, 480)

Approach #7

@driver.execute_script("window.resizeTo(640, 480);")
Phiz answered 8/11, 2018 at 10:37 Comment(0)
R
2

If you're using the Facebook language binding for php try this:

$driver->manage()->window()->setSize(new WebDriverDimension(1024,768));
Raymonraymond answered 4/5, 2016 at 19:31 Comment(0)
B
2

In java/groovy try:

import java.awt.Toolkit;
import org.openqa.selenium.Dimension;         
import org.openqa.selenium.Point;

...

java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension maximizedScreenSize = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());
driver.manage().window().setPosition(new Point(0, 0));
driver.manage().window().setSize(maximizedScreenSize);

this will open browser in fullscreen

Bernini answered 9/9, 2016 at 13:12 Comment(0)
I
2

Following chrome options worked for me for headless chrome:

IN JAVA:

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--window-size=1920,1080");
chromeOptions.setHeadless(true);
driver = new ChromeDriver(chromeOptions);
driver.get("your-site");
driver.manage().window().maximize();

selenium-java: 3.8.1

chromedriver: 2.43

Chrome: v69-71

Ibo answered 30/10, 2018 at 8:42 Comment(0)
C
0

If you are using Clojure and https://github.com/semperos/clj-webdriver you can use this snippet to resize the browser.

(require '[clj-webdriver.taxi :as taxi])

; Open browser
(taxi/set-driver! {:browser :chrome} "about:blank")

; Resize browser
(-> taxi/*driver* (.webdriver) (.manage) (.window) 
  (.setSize (org.openqa.selenium.Dimension. 0 0)))
Carrycarryall answered 10/1, 2017 at 2:46 Comment(0)
P
0

Use Dimension Class for controlling window size.

Dimension d = new Dimension(1200,800);  //(x,y coordinators in pixels) 
driver.manage().window().setSize(d);
Pitfall answered 11/1, 2017 at 4:56 Comment(0)
C
0

Ruby version:

caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args"=> ["--window-size=1280,960"]})

url = "http://localhost:9515"  # if you are using local chrome or url = Browserstack/ saucelabs hub url if you are using cloud service providers.

 Selenium::WebDriver.for(:remote, :url => url, :desired_capabilities => caps)

resize chrome is buggy in latest chromedrivers , it fails intermittanly with this failure message.

                        unknown error: cannot get automation extension
from timeout: cannot determine loading status
from timeout: Timed out receiving message from renderer: -65.294
  (Session info: chrome=56.0.2924.76)
  (Driver info: chromedriver=2.28.455517 (2c6d2707d8ea850c862f04ac066724273981e88f)

And resizeTo via javascript also not supported for chrome started via selenium webdriver. So the last option was using command line switches.

Croydon answered 20/4, 2017 at 10:58 Comment(0)
L
0

As long as the total screen resolution you are using is larger than the window size you want to use you can use this (C#):

browserDriver.Manage().Window.Size = windowSize;

If your screen resolution is smaller then you are out of luck. I tried different ways such as:

chromeOptions.AddArgument($"window-size={windowSize.Width},{windowSize.Height}");

Or even importing the move window function to resize a window:

[DllImport("user32.dll", SetLastError = true)]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int Width, int Height, bool Repaint);

// Example
MoveWindow(process.MainWindowHandle, 0, 0, windowSize.Width, windowSize.Height, true);

But none of these work. It seems that chrome does something different to prevent the window size from growing bigger than the screen resolution. With Firefox I didn't see this behavior.

Lei answered 4/10, 2021 at 9:2 Comment(0)
A
0

From my experience setting size from the webdriver is not always reliable. It might apply it to the viewport only or might fall into other issues (like minimum screen size for that particular webdriver).

See https://w3c.github.io/webdriver/#dfn-set-window-rect

The specification does not guarantee that the resulting window size will exactly match that which was requested. In particular the implementation is expected to clamp values that are larger than the physical screen dimensions, or smaller than the minimum window size.

I'm going to try with setting capabilites to see if it's any better.

Allegory answered 13/11, 2021 at 9:22 Comment(0)
S
0

The screen resolution of my device was lower than the screen resolution expected by the test.

Workaround that worked for me:

options.EnableMobileEmulation(new ChromiumMobileEmulationDeviceSettings
{
   Height = 1080,
   Width = 1920,
   EnableTouchEvents = false,
   UserAgent = "Responsive"
});
Signalment answered 17/7, 2023 at 11:59 Comment(0)
P
0

In Python, you can set window size for Chrome with Selenium as shown below. *-window-size and window-size also work:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_argument("--window-size=1024,768")
driver = webdriver.Chrome(options=options)

And in Python, you can also do it with the code below according to the doc:

from selenium import webdriver

driver = webdriver.Chrome()
driver.set_window_size(1024, 768)

*My answer explains it more.

Preliminaries answered 12/9, 2023 at 6:42 Comment(0)
V
0

update 12/2023

ChromeOptions options = new ChromeOptions(); options.AddArgument("--app=http:// abc. com ");

//options.AddArgument("--window-size=400,500"); //if this config does not work, try Window size bellow

ChromeDriver webBrowser = new ChromeDriver(options);

webBrowser.Manage().Window.Size = new Size(400,500);

Vaporization answered 7/12, 2023 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.