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

如何让Selenium Chrome Driver启动时最小化或隐藏?

解决方案

方法1:启动时直接最小化窗口

给ChromeOptions添加--start-minimized启动参数,浏览器启动后会自动处于最小化状态,无需后续手动调用driver.minimize_window(),彻底避免窗口短暂弹出的干扰。

修改后的代码:

import undetected_chromedriver.v2 as uc

def start_uc():
    '''opens a UC chrome driver with prefered settings'''
    options = uc.ChromeOptions()
    # 跳过弹窗的参数 + 启动时最小化
    options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
    options.add_argument('--start-minimized')  # 新增启动时最小化参数
    return uc.Chrome(options=options)

driver = start_uc()

for item in list:
    # 爬取 item 信息的逻辑

    if blocked from page:
        driver.close()
        driver = start_uc()

方法2:启动时将窗口移至屏幕外

如果希望窗口完全隐藏在可视区域外(等效于启动后执行driver.set_window_position(-2000,0)),可以添加--window-position=-2000,0参数,启动后窗口会直接处于屏幕左侧外的位置,完全不会干扰操作。

修改后的代码:

import undetected_chromedriver.v2 as uc

def start_uc():
    '''opens a UC chrome driver with prefered settings'''
    options = uc.ChromeOptions()
    options.add_argument('--no-first-run --no-service-autorun --password-store=basic')
    options.add_argument('--window-position=-2000,0')  # 新增窗口位置参数
    return uc.Chrome(options=options)

driver = start_uc()

for item in list:
    # 爬取 item 信息的逻辑

    if blocked from page:
        driver.close()
        driver = start_uc()

内容的提问来源于stack exchange,提问作者Christian Trujillo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 20:00:39