使用pyinstaller将.py转换为.exe后,带有代理的Selenium脚本不起作用

时间:2020-10-03 11:43:17

标签: selenium selenium-webdriver selenium-chromedriver 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

1 个答案:

答案 0 :(得分:1)

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

如果它可以与脚本中的代理一起使用,我认为这里的问题可能是其中之一,或者可能是两者

相关问题