Python 如何在 Selenium WebDriver 中设置浏览器宽度和高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15397483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:01:37  来源:igfitidea点击:

How do I set browser width and height in Selenium WebDriver?

pythonseleniumselenium-webdriverscreen-resolution

提问by tom

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:

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

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 foowould be, and I can't figure out where the docs are.

但我不知道foo会是什么,我无法弄清楚文档在哪里。

Q1:is there a way to set width / height at instantiation?

Q1:有没有办法在实例化时设置宽度/高度?

Q2:Where are the reference docs listing all keys usable by profile.set_preference?

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

回答by RoneRackal

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

不确定它是否会在启动后调整大小,所以也许这不是你想要的

回答by TONy.W

Here is firefox profile default prefs from python selenium 2.31.0 firefox_profile.py

这是来自 python selenium 2.31.0 firefox_profile.py 的 firefox 配置文件默认首选项

and type "about:config" in firefox address bar to see all prefs

并在 Firefox 地址栏中键入“about:config”以查看所有首选项

reference to the entries in about:config: http://kb.mozillazine.org/About:config_entries

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

回答by Deniz Kaplan

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.

将此代码写入测试代码的设置部分,在:webdriver.Firefox()行之前。

回答by Steve HHH

For me, the only thing that worked in Java 7 on OS X 10.9 was this:

对我来说,在 OS X 10.9 上的 Java 7 中唯一有效的是:

// driver = new RemoteWebDriver(new URL(grid), capability);
driver.manage().window().setPosition(new Point(0,0));
driver.manage().window().setSize(new Dimension(1024,768));

Where 1024is the width, and 768is the height.

哪里1024是宽度,哪里是768高度。

回答by sirex

Here is how I do it in Python with Selenium 2.48.0:

这是我在 Python 中使用 Selenium 2.48.0 的方法:

from selenium.webdriver import Firefox
driver = Firefox()
driver.set_window_position(0, 0)
driver.set_window_size(1024, 768)

回答by e4c5

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.

这将导致浏览器在第二台显示器上启动,而不会在屏幕上出现任何烦人的闪烁或移动。

回答by Raj Sahoo

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.

确保 chrome 驱动程序在您的系统路径中。

回答by tsorn

This works both with headlessand non-headless, and will start the window with the specified size instead of setting it after:

这适用于headless无头和无头,并将以指定的大小启动窗口,而不是在以下之后设置它:

from selenium.webdriver import Firefox, FirefoxOptions

opts = FirefoxOptions()
opts.add_argument("--width=2560")
opts.add_argument("--height=1440")

driver = Firefox(options=opts)