使用 pyinstaller 将 .py 转换为 .exe 后,带代理的 Selenium 脚本无法正常工作

Selenium Script with proxy is not working after converting .py to .exe using pyinstaller

希望你们一切都好。 我写了一个 python 脚本,它使用经过身份验证的代理,现在我已经将它转换为 .exe,它给我错误“ERROR TUNNEL Connection”。 所以 .exe 文件的行为是,

  1. 没有代理,它工作正常
  2. 使用代理时,出现“错误隧道连接”

这里有人知道解决办法吗?

def get_proxy_variables(PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS):
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']
    );
    """ % (PROXY_HOST, PROXY_PORT, PROXY_USER, PROXY_PASS)
return manifest_json, background_js


def get_chromedriver(use_proxy=False, user_agent=None):
proxy = get_random_proxy()
print(proxy)
manifest_json, background_json = get_proxy_variables(proxy[0], proxy[1], proxy[2], proxy[3])
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--start-maximized")
if use_proxy:
    pluginfile = 'proxy_auth_plugin.zip'
    with zipfile.ZipFile(pluginfile, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_json)
    chrome_options.add_extension(pluginfile)
if user_agent:
    chrome_options.add_argument('--user-agent=%s' % user_agent)
driver = webdriver.Chrome(options=chrome_options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
    "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})
return driver

您是否尝试过以管理员权限启动或在 windows 防火墙设置中允许它?

如果它与脚本中的代理一起工作,我认为这里的问题可能是其中之一,也可能是两者