How to set browser's width and height in Selenium WebDriver?
Asked Answered
M

10

115

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?

Melany answered 13/3, 2013 at 22:10 Comment(3)
Why is it a bad thing that it resizes after being created?Cultigen
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.Melany
I would agree with Arran. I can't think of any reason why resize after creation would cause issues.Nonrigid
C
68

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)
Cocci answered 27/4, 2019 at 8:18 Comment(2)
The equivalent in Chrome is "--window-size=2560,1440".Delija
THIS is the right answer here, thanksDonielle
S
55

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)
Simmer answered 15/3, 2016 at 11:43 Comment(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.Slone
set_window_position(0,0) positions the window about 10 pixels off the left of screen annoyingly.Nucleoplasm
L
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.

Linzy answered 23/2, 2014 at 6:20 Comment(0)
M
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

Mydriatic answered 13/3, 2013 at 22:16 Comment(1)
Isn't it suppose to be pythonGlorianna
S
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.

Summer answered 11/5, 2016 at 0:20 Comment(1)
Do you know how to do the same trick in Firefox? Maybe you can answer my question.Oribel
H
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.

Human answered 12/10, 2017 at 13:59 Comment(3)
the question requires you to set the size before instantiating the driverLaoag
This works for meNoisy
This only happens after the window is loaded.Burton
F
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",
    }
Furious answered 14/3, 2013 at 16:53 Comment(1)
Thanks. I dont see browser size settings in there, but it's useful to have the list and the link all the same.Melany
E
1

<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)
Eupatrid answered 12/9, 2023 at 2:46 Comment(0)
A
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.

Antimatter answered 28/8, 2013 at 9:16 Comment(1)
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.5Multilateral
A
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);
Ashur answered 9/9, 2021 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.