118

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?

3
  • 7
    Why is it a bad thing that it resizes after being created? Commented Mar 14, 2013 at 9:18
  • 3
    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. Commented Mar 14, 2013 at 20:49
  • 2
    I would agree with Arran. I can't think of any reason why resize after creation would cause issues. Commented Aug 28, 2013 at 9:37

11 Answers 11

71

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)
Sign up to request clarification or add additional context in comments.

1 Comment

The equivalent in Chrome is "--window-size=2560,1440".
56

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)

2 Comments

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.
set_window_position(0,0) positions the window about 10 pixels off the left of screen annoyingly.
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.

Comments

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

1 Comment

Isn't it suppose to be python
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.

1 Comment

Do you know how to do the same trick in Firefox? Maybe you can answer my question.
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.

3 Comments

the question requires you to set the size before instantiating the driver
This works for me
This only happens after the window is loaded.
3

<Chrome>

*-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)

*This code below also works according to the doc:

from selenium import webdriver

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

<Microsoft Edge>

*-window-size and window-size also work:

from selenium import webdriver

options = webdriver.EdgeOptions()
options.add_argument("--window-size=1024,768")
driver = webdriver.Edge(options=options)

*This code below also works according to the doc:

from selenium import webdriver

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

<Firefox>

*-width and -height also work while width and height don't work:

from selenium import webdriver

options = webdriver.FirefoxOptions()
options.add_argument("--window-size=1024,768")
driver = webdriver.Firefox(options=options)

*This code below also works according to the doc:

from selenium import webdriver

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

Comments

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",
    }

1 Comment

Thanks. I dont see browser size settings in there, but it's useful to have the list and the link all the same.
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.

1 Comment

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
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);

Comments

0

These worked for me in Safari...

from selenium import webdriver
...

driver = webdriver.Safari()

then, for...

Window size only:

driver.set_window_size(width, height)

Window size and position:

driver.set_window_rect(x, y, width, height)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.