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

基于Python的Selenium带认证代理配置及多窗口语言问题

Selenium带认证代理与强制巴西葡语窗口的问题

我需要在Selenium中使用带认证的代理,但Selenium本身缺乏实用的认证代理方案,仅支持无认证代理——配置带认证代理参数时会弹出账号密码输入框,这无法满足我同时打开10个不同位置、不同大小页面的需求。

已尝试的解决方案及问题

  • Selenium-wire:运行速度极慢,打包为EXE时被识别为病毒,无法完成全部进程,仅部分窗口正常工作
  • Seleniumbase的Driver():功能正常,但无法定位打开的页面,且页面打开速度过慢
  • Selenium原生旧代码:DesiredCapabilities相关功能已变更,无法正常运行,代码如下:
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium import webdriver

proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = "username:password@ip:port"
proxy.ssl_proxy = "username:password@ip:port"  
capabilities = webdriver.DesiredCapabilities.CHROME.copy()
proxy.add_to_capabilities(capabilities)
  • Chrome扩展配置代理:该方案运行良好,但部分窗口无法按需求以葡萄牙语打开,语言显示混乱(时而葡萄牙语时而英语)。我的用户均为巴西葡语使用者,需确保窗口强制以葡萄牙语打开,相关代码如下:
def create_chromedriver(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS, USER_AGENT, pos_x, pos_y, altura, largura, posicao):
    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }
    """

    background_js = """
    var config = {
        mode: "fixed_servers",
        rules: {
        singleProxy: {
            scheme: "http",
            host: "%s",
            port: parseInt(%s)
        },
        bypassList: ["localhost"]
        }
    };

    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

    function callbackFn(details) {
        return {
            authCredentials: {
                username: "%s",
                password: "%s"
            }
        };
    }

    chrome.webRequest.onAuthRequired.addListener(
            callbackFn,
            {urls: ["<all_urls>"]},
            ['blocking']
    );

    chrome.webRequest.onBeforeSendHeaders.addListener(
        function(details) {
            for (var i = 0; i < details.requestHeaders.length; ++i) {
                if (details.requestHeaders[i].name === 'Accept-Language') {
                    details.requestHeaders[i].value = 'pt-BR,pt;q=0.9';
                    break;
                }
            }
            return {requestHeaders: details.requestHeaders};
        },
        {urls: ["<all_urls>"]},
        ["blocking", "requestHeaders"]
    );

    """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)

    def get_chromedriver(use_proxy=True, user_agent=USER_AGENT, x=pos_x, y=pos_y, alt=altura, larg=largura, posicao_janela=posicao):
        path = os.path.dirname(os.path.abspath(__file__))
        chrome_options = Options()

        if use_proxy:
            pluginfile = f'proxy_{posicao_janela}.zip'
            with zipfile.ZipFile(pluginfile, 'w') as zp:
                zp.writestr("manifest.json", manifest_json)
                zp.writestr("background.js", background_js)
            chrome_options.add_extension(pluginfile)
        
        #if user_agent:
        #    chrome_options.add_argument('--user-agent=%s' % USER_AGENT)

        arguments = ['--disable-notifications', '--disable-popup-blocking', f"--window-position={x},{y}", f"--window-size={larg},{alt}"]

        for argument in arguments:
            chrome_options.add_argument(argument)

        driver = webdriver.Chrome(options=chrome_options)
        return driver

    driver = get_chromedriver(use_proxy=True)
    return driver

内容的提问来源于stack exchange,提问作者Felipe Ataide

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 23:38:15