52

I tried setting the browser size on Chrome --headless by using Selenium WebDriver commands.

I get this WebDriver error:

      - Failed: unknown error: cannot get automation extension
from unknown error: page could not be found: chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
  (Session info: headless chrome=58.0.3029.81)
  (Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 4.4.0-72-generic x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.07 seconds
Build info: version: '3.3.1', revision: '5234b32', time: '2017-03-10 09:04:52 -0800'
System info: host: '826f6a766112', ip: '172.17.0.2', os.name: 'Linux', os.arch: 'amd64', os.version: '4.4.0-72-generic', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5), userDataDir=/tmp/.org.chromium.Chromium.cuymDL}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.81, platform=LINUX, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 9569e5ebd8f7540ce510b20647443baf
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
  • Can you provide the following info: 1. Which headless browser are you using HtmlUnitDriver or PhantomJSDriver ? 2. Can you share the url you are trying to access? 3. What are your steps? 4. What error are you observing? – undetected Selenium Apr 21 '17 at 11:58
  • No Phantom, this is the real Chrome browser, truly headless. I found the answer, see below. – Leo Gallucci Apr 21 '17 at 12:03

5 Answers5

96

I found it. Simply pass the --window-size command line argument to Google Chrome, for example --window-size=1920,1080.

In a Protractor configuration this would look like this:

capabilities: {
    browserName: 'chrome',
    chromeOptions: {
        args: ['headless', 'window-size=1920,1080']
    }
}

The cool thing is that the windows size is not limited to the current display. It is truly headless, meaning it can be as large as needed for the tests.

Java code:

options.addArguments("window-size=1920,1080");

I expand a bit more on this in Headless protractor not sharding tests.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Leo Gallucci
  • 16,355
  • 12
  • 77
  • 110
9

Use the built-in Selenium function:

    aDriver.manage().window().setSize(new Dimension(width, height));

It works like a champ. I've used it for Firefox, Chrome (even headless), and Edge.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MivaScott
  • 1,763
  • 1
  • 12
  • 30
  • 1
    python+splinter: browser.driver.set_window_size(width, height) – mirek Mar 21 '18 at 19:06
  • 1
    It is not working in headless mode. Mr. Leo's answer is working like charm. same in c# var options = new ChromeOptions(); options.AddArgument("no-sandbox"); options.AddArgument("headless"); options.addArguments("window-size=1920,1080"); IWebDriver _driver= new ChromeDriver($@"path to chrome web driver folder", options, TimeSpan.FromSeconds(130)); – Shakoor Hussain Attari Jun 07 '21 at 05:40
2

Mr. Leo's answer is working like charm. and i've tested this with 6000x6000 window size.

Same in c#

var options = new ChromeOptions(); 
options.AddArgument("no-sandbox");
options.AddArgument("headless");
options.addArguments("window-size=1920,1080");
IWebDriver _driver= new ChromeDriver($@"path to chrome web driver folder", options, TimeSpan.FromSeconds(130));
Diego Montania
  • 322
  • 5
  • 12
2

I had the question in Python, so here's the Python solution:

options = Options()
options.headless = True
options.add_argument("window-size=1920,1080");
driver = webdriver.Chrome(options=options)
DonCarleone
  • 544
  • 11
  • 20
1

I had a problem with screen resolution when running on Jenkins (which forces headless mode). The solution was very simple: set headless mode to true explicitly. For some reason, not doing this explicitly caused my screen to "shrink" causing all kinds of "element intercept click" issues. Because I was taking screenshots during failures, I noticed the resolution (size) was not right. Once I did this, the problem went away; confirmed by screenshots taken during failures.

To avoid conflicts with local configurations, I moved the value of this flag into a configuration file that was then added to our .gitignore file to prevent accidental overwrites.

If you are like me, where none of these commonly solutions worked out, make sure you explicitly set this value:

ChromeOptions options = new ChromeOptions();
...
String headlessMode = readProperty("headless_mode"); // Get value from some prop file (my own implementation)
options.setHeadless(Boolean.valueOf(headlessMode));
...
driver = new ChromeDriver(options);

If you don't want (or need) this separation, simply set the value to true in the setHeadless method call.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hfontanez
  • 5,774
  • 2
  • 25
  • 37