Reputation: 2299
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
?
Upvotes: 118
Views: 197208
Reputation: 8749
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)
Upvotes: 0
Reputation: 1
*-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)
*-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)
*-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)
Upvotes: 3
Reputation: 3625
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:
from selenium.webdriver import Chrome, ChromeOptions
opts = ChromeOptions()
opts.add_argument("--window-size=2560,1440")
driver = Chrome(options=opts)
from selenium.webdriver import Firefox, FirefoxOptions
opts = FirefoxOptions()
opts.add_argument("--width=2560")
opts.add_argument("--height=1440")
driver = Firefox(options=opts)
Upvotes: 70
Reputation: 1
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);
Upvotes: 0
Reputation: 496
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.
Upvotes: 4
Reputation: 53774
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.
Upvotes: 13
Reputation: 4911
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)
Upvotes: 56
Reputation: 13147
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.
Upvotes: 46
Reputation: 1609
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.
Upvotes: 0
Reputation: 1946
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",
}
Upvotes: 1
Reputation: 1233
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
Upvotes: 19