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

height, python, selenium-webdriver, width, window-size

Solution

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)

Problem

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`?

Original source