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

使用Proxy时Selenium Driver自动关闭的问题求助

Selenium Edge代理配置后页面立即关闭的解决方案
  • 改用EdgeOptions配置代理(更可靠的方式)
    直接修改DesiredCapabilities可能存在兼容性问题,推荐用EdgeOptions类来配置代理,这是当前Selenium官方更推荐的写法:

    from selenium import webdriver
    from selenium.webdriver.edge.options import Options
    
    proxy_host = 'proxy.froxy.com'
    proxy_port = '9000'
    
    edge_options = Options()
    # 同时设置HTTP和HTTPS代理
    edge_options.add_argument(f'--proxy-server=http://{proxy_host}:{proxy_port}')
    
    with webdriver.Edge(options=edge_options) as driver:
        driver.get("https://www.SomeDomain.com/account/login")
        
        # 这里可以添加后续业务操作,比如等待元素加载
        from selenium.webdriver.support.ui import WebDriverWait
        from selenium.webdriver.support import expected_conditions as EC
        from selenium.webdriver.common.by import By
    
        # 示例:等待登录按钮出现(10秒超时)
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "login-button"))
        )
        # 执行输入账号密码等操作
        # driver.find_element(By.ID, "username").send_keys("你的账号")
        # driver.find_element(By.ID, "password").send_keys("你的密码")
        
        # 用用户输入替代死循环,临时保持页面打开
        input("操作完成后按回车关闭浏览器...")
    
  • 代理需要认证的处理方案
    如果你的代理要求用户名密码验证,直接用--proxy-server参数无法生效,可以借助selenium-wire库实现:

    # 先安装依赖:pip install selenium-wire
    from seleniumwire import webdriver
    from seleniumwire.edge.options import Options
    
    edge_options = Options()
    seleniumwire_options = {
        'proxy': {
            'http': f'http://你的用户名:你的密码@{proxy_host}:{proxy_port}',
            'https': f'http://你的用户名:你的密码@{proxy_host}:{proxy_port}',
            'no_proxy': 'localhost,127.0.0.1'  # 本地地址不走代理
        }
    }
    
    with webdriver.Edge(options=edge_options, seleniumwire_options=seleniumwire_options) as driver:
        driver.get("https://www.SomeDomain.com/account/login")
        # 后续操作...
        input("按回车关闭浏览器...")
    
  • 替代死循环的页面保持技巧
    用input()函数让程序暂停,等待用户输入后再继续执行——既不会像死循环那样完全阻塞解释器,又能在暂停期间保留Selenium会话,方便你完成手动操作或后续代码执行。

  • 排查代理本身的问题

    • 先测试代理能否正常访问目标站点:用curl -x proxy.froxy.com:9000 https://www.SomeDomain.com/account/login命令验证,看是否能正常返回页面内容。
    • 确认代理没有被公司站点的防火墙拦截,或者是否需要将代理IP加入站点白名单。

内容的提问来源于stack exchange,提问作者Leo Torres

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 20:32:46