如何在 SeleniumWebDriver 中设置浏览器的宽度和高度?

我正在使用 Selenium WebDriver for Python。 我想用一个特定的宽度和高度来实例化浏览器,目前我能得到的最接近的结果是:

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

它可以工作,但是要在创建之后设置浏览器的大小,我希望在实例化时设置它。我猜有一种方法是这样的:

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

但我不知道 foo是什么我也不知道文件在哪里。

Q1: 有没有在实例化时设置 width/height 的方法?

问题2: 列出 profile.set_preference可用的所有密钥的参考文档在哪里?

151271 次浏览

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

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

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.

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)

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.

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.

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)

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