109

I'm using Selenium WebDriver for Python. I want instantiate the browser with a specific width and height. So far the closest I can get is:

driver = webdriver.Firefox()
driver.set_window_size(1080,800)

Which works, but sets the browser size after it is created, and I want it set at instantiation. I'm guessing there is an approach along the lines of:

profile = webdriver.FirefoxProfile();
profile.set_preference(foo, 1080)
driver = webdriver.Firefox(profile)

But I don't know what foo would be, and I can't figure out where the docs are.

Q1: is there a way to set width / height at instantiation?

Q2: Where are the reference docs listing all keys usable by profile.set_preference?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
tom
  • 2,189
  • 2
  • 15
  • 27
  • 6
    Why is it a bad thing that it resizes after being created? – Arran Mar 14 '13 at 09:18
  • 2
    Honestly, I don't know. Strictly speaking it's different to a window being opened at a specific size (I can **see** the difference). I don't know whether that difference is _material_. Seeing as these are tests, I'd like to remove as many unknowns as possible. If someone can tell me that it makes no difference then I'd be happy. But they'd probably need to know the specifics of my test to do that, and I want to be able to do this for all future tests that I write. – tom Mar 14 '13 at 20:49
  • 2
    I would agree with Arran. I can't think of any reason why resize after creation would cause issues. – Robbie Wareham Aug 28 '13 at 09:37

9 Answers9

64

Here's a solution that works with both headless and non-headless mode and will start the window with the specified size instead of setting it after:

Chrome:

from selenium.webdriver import Chrome, ChromeOptions

opts = ChromeOptions()
opts.add_argument("--window-size=2560,1440")

driver = Chrome(options=opts)

Firefox:

from selenium.webdriver import Firefox, FirefoxOptions

opts = FirefoxOptions()
opts.add_argument("--width=2560")
opts.add_argument("--height=1440")

driver = Firefox(options=opts)
tsorn
  • 3,365
  • 1
  • 29
  • 48
53

Here is how I do it in Python with Selenium 2.48.0:

from selenium.webdriver import Firefox
driver = Firefox()
driver.set_window_position(0, 0)
driver.set_window_size(1024, 768)
sirex
  • 4,593
  • 2
  • 32
  • 21
  • 2
    The OP explicitly says they don't want to set it after creation. The code in the question already shows them using `set_window_size`. – Bryan Oakley Mar 05 '19 at 23:07
  • `set_window_position(0,0)` positions the window about 10 pixels off the left of screen annoyingly. –  Oct 19 '20 at 18:12
46

For me, the only thing that worked in Java 7 on OS X 10.9 was this:

// driver = new RemoteWebDriver(new URL(grid), capability);
driver.manage().window().setPosition(new Point(0,0));
driver.manage().window().setSize(new Dimension(1024,768));

Where 1024 is the width, and 768 is the height.

Steve HHH
  • 12,947
  • 6
  • 68
  • 71
19

Try something like this:

IWebDriver _driver = new FirefoxDriver();
_driver.Manage().Window.Position = new Point(0, 0);
_driver.Manage().Window.Size = new Size(1024, 768);

Not sure if it'll resize after being launched though, so maybe it's not what you want

RoneRackal
  • 1,243
  • 2
  • 10
  • 16
13

If you are using chrome

 chrome_options = Options()
 chrome_options.add_argument("--start-maximized");
 chrome_options.add_argument("--window-position=1367,0");
 if mobile_emulation :
     chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

  self.driver = webdriver.Chrome('/path/to/chromedriver', 
                                  chrome_options = chrome_options)

This will result in the browser starting up on the second monitor without any annoying flicker or movements across the screen.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Do you know how to do the same trick in Firefox? Maybe you can answer [my question](https://stackoverflow.com/q/54575853/8973620). – Mykola Zotko Feb 08 '19 at 19:32
4

It's easy. Here is the full code.

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("Your URL")
driver.set_window_size(480, 320)

Make sure chrome driver is in your system path.

Raj Sahoo
  • 488
  • 6
  • 11
1

Here is firefox profile default prefs from python selenium 2.31.0 firefox_profile.py

and type "about:config" in firefox address bar to see all prefs

reference to the entries in about:config: http://kb.mozillazine.org/About:config_entries

DEFAULT_PREFERENCES = {
    "app.update.auto": "false",
    "app.update.enabled": "false",
    "browser.download.manager.showWhenStarting": "false",
    "browser.EULA.override": "true",
    "browser.EULA.3.accepted": "true",
    "browser.link.open_external": "2",
    "browser.link.open_newwindow": "2",
    "browser.offline": "false",
    "browser.safebrowsing.enabled": "false",
    "browser.search.update": "false",
    "extensions.blocklist.enabled": "false",
    "browser.sessionstore.resume_from_crash": "false",
    "browser.shell.checkDefaultBrowser": "false",
    "browser.tabs.warnOnClose": "false",
    "browser.tabs.warnOnOpen": "false",
    "browser.startup.page": "0",
    "browser.safebrowsing.malware.enabled": "false",
    "startup.homepage_welcome_url": "\"about:blank\"",
    "devtools.errorconsole.enabled": "true",
    "dom.disable_open_during_load": "false",
    "extensions.autoDisableScopes" : 10,
    "extensions.logging.enabled": "true",
    "extensions.update.enabled": "false",
    "extensions.update.notifyUser": "false",
    "network.manage-offline-status": "false",
    "network.http.max-connections-per-server": "10",
    "network.http.phishy-userpass-length": "255",
    "offline-apps.allow_by_default": "true",
    "prompts.tab_modal.enabled": "false",
    "security.fileuri.origin_policy": "3",
    "security.fileuri.strict_origin_policy": "false",
    "security.warn_entering_secure": "false",
    "security.warn_entering_secure.show_once": "false",
    "security.warn_entering_weak": "false",
    "security.warn_entering_weak.show_once": "false",
    "security.warn_leaving_secure": "false",
    "security.warn_leaving_secure.show_once": "false",
    "security.warn_submit_insecure": "false",
    "security.warn_viewing_mixed": "false",
    "security.warn_viewing_mixed.show_once": "false",
    "signon.rememberSignons": "false",
    "toolkit.networkmanager.disable": "true",
    "toolkit.telemetry.enabled": "false",
    "toolkit.telemetry.prompted": "2",
    "toolkit.telemetry.rejected": "true",
    "javascript.options.showInConsole": "true",
    "browser.dom.window.dump.enabled": "true",
    "webdriver_accept_untrusted_certs": "true",
    "webdriver_enable_native_events": "true",
    "webdriver_assume_untrusted_issuer": "true",
    "dom.max_script_run_time": "30",
    }
TONy.W
  • 1,948
  • 19
  • 10
  • 1
    Thanks. I dont see browser size settings in there, but it's useful to have the list and the link all the same. – tom Mar 14 '13 at 20:50
0
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.window.width',0)
profile.set_preference('browser.window.height',0)
profile.update_preferences()

write this code into setup part of your test code, before the: webdriver.Firefox() line.

Max
  • 12,622
  • 16
  • 73
  • 101
Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18
  • 2
    The question is about setting the initial windows size using **python**.... Anyway, this doesn't work either using python, at least using selenium 2.46.0 and Firefox 38.0.5 – Álvaro Arranz Jul 01 '15 at 15:29
0

I tried the below code to set the resolution on mac chrome and windows chrome.

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

Use the below code to test the resolution changes:-

Dimension dimension = driver.manage().window().getSize();
LOGGER.info("dimension is " + dimension);