You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium geckodriver使用--window-size设置Firefox窗口大小失效问题

问题根因

--window-size 是Chromium系浏览器专属的启动参数,Gecko内核的Firefox原生不识别该参数,传入后会被直接忽略。该问题和你当前使用的Selenium、geckodriver、Firefox版本无关,纯粹是两款浏览器的启动参数体系不通用导致的。

可行解决方案

Firefox + geckodriver 环境下有两种稳定的窗口尺寸设置方案:

方案1:使用Selenium通用API设置(跨浏览器兼容)

该方式不依赖浏览器专属参数,Chrome、Firefox均能正常生效,是最推荐的跨浏览器自动化实现方式:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

geckodriver_path = Service('/usr/bin/geckodriver')
driver = webdriver.Firefox(service=geckodriver_path)
# 传入参数顺序为宽度、高度,单位为像素
driver.set_window_size(800, 800)

driver.get("http://www.wikipedia.org")
print(driver.get_window_size())

方案2:通过Firefox配置项在启动阶段固定尺寸

如果需要浏览器启动瞬间就应用目标尺寸,避免启动后出现窗口大小跳变,可以通过Firefox偏好配置实现:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service

geckodriver_path = Service('/usr/bin/geckodriver')
firefox_options = Options()
# 配置Firefox启动默认窗口宽高
firefox_options.set_preference("browser.width", 800)
firefox_options.set_preference("browser.height", 800)
driver = webdriver.Firefox(service=geckodriver_path, options=firefox_options)

driver.get("http://www.wikipedia.org")
print(driver.get_window_size())
补充说明
  • 你当前使用的Selenium 4.1.0、geckodriver 0.31.0、Firefox 101.0.1版本组合不存在窗口设置相关的已知兼容性问题,无需升级版本即可实现需求
  • 编写跨浏览器自动化脚本时,优先使用Selenium封装的通用API操作浏览器,不要硬依赖某一款浏览器的专属启动参数,避免出现单浏览器生效、其他环境失效的问题

内容的提问来源于stack exchange,提问作者Boris L.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 13:21:26