How can I control Chromedriver open window size?
SeleniumSelenium ChromedriverSelenium Problem Overview
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?
Selenium Solutions
Solution 1 - Selenium
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)
Solution 2 - Selenium
Use this for your custom size:
driver.manage().window().setSize(new Dimension(1024,768));
you can change your dimensions as per your requirements.
Solution 3 - Selenium
#use chrome webdriver
driver = webdriver.Chrome('path to /chromedriver')
driver.set_window_size(1400,1000)
Solution 4 - Selenium
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))
{
...
}
Solution 5 - Selenium
try this
using System.Drawing;
driver.Manage().Window.Size = new Size(width, height);
Solution 6 - Selenium
Try with driver.manage.window.maximize();
to maximize window.
Solution 7 - Selenium
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);")
Solution 8 - Selenium
If you're using the Facebook language binding for php try this:
$driver->manage()->window()->setSize(new WebDriverDimension(1024,768));
Solution 9 - Selenium
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
Solution 10 - Selenium
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
Solution 11 - Selenium
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)))
Solution 12 - Selenium
Use Dimension Class
for controlling window size.
Dimension d = new Dimension(1200,800); //(x,y coordinators in pixels)
driver.manage().window().setSize(d);
Solution 13 - Selenium
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.
Solution 14 - Selenium
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.
Solution 15 - Selenium
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.