【问题标题】:How can i set proxy with authentication in selenium chrome web driver using python如何使用 python 在 selenium chrome web 驱动程序中设置具有身份验证的代理
【发布时间】:2015-07-11 01:30:48
【问题描述】:

我有以下脚本可以使用 python selenium Chrome 驱动程序访问网页。

from selenium import webdriver
USERNAME = 'usename'
PASSWORD = 'pass'
proxies = ["xxx.xxx.xxx.xxx"]
proxy_tpl ='{0}:{1}'
proxy = proxy_tpl.format(proxies[0],'xx') 
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxy)
chrome = webdriver.Chrome(chrome_options=chrome_options)
chrome.get("http://{0}:{1}@whatismyipaddress.com".format(USERNAME, PASSWORD))
driver.close()

当我尝试运行脚本时,Chrome 仍然询问用户名和密码。如何通过脚本验证代理服务器?

【问题讨论】:

    标签: python python-2.7 google-chrome selenium proxy


    【解决方案1】:

    受此solution in php 的启发,我在 python 中编写了一个等价物:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import zipfile
    
    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: "XXX.XXX.XXX.XXX",
                port: parseInt(XXXX)
              },
              bypassList: ["foobar.com"]
            }
          };
    
    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
    
    function callbackFn(details) {
        return {
            authCredentials: {
                username: "XXXXXXXXX",
                password: "XXXXXXXXX"
            }
        };
    }
    
    chrome.webRequest.onAuthRequired.addListener(
                callbackFn,
                {urls: ["<all_urls>"]},
                ['blocking']
    );
    """
    
    
    pluginfile = 'proxy_auth_plugin.zip'
    
    with zipfile.ZipFile(pluginfile, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)
    
    co = Options()
    co.add_argument("--start-maximized")
    co.add_extension(pluginfile)
    
    
    driver = webdriver.Chrome("path/to/chromedriver",  chrome_options=co)
    driver.get("http://www.google.com.br")
    

    background_js 字符串中,将 XXX 替换为您的信息。

    【讨论】:

    • 您好,感谢您提供此代码。有效。我被困在另一个地方。我正在尝试使用 OAuth2.0 身份验证登录自己,并且我在我的本地主机上执行此操作,所以我在我的 redirect_uri 中设置了本地主机。但是由于这个代理浏览器无法访问本地主机。你能建议我访问同样的东西吗?
    • 我想问一下 {urls: [""]} 是否需要任何修改或按原样工作。我不清楚。
    • @AbdulKhalid 您是否尝试将 localhost 明确放入权限中?
    • @user3286105 它期望的网址是什么?
    • 请注意,如果您在隐身模式下启动 chrome,此解决方案将不起作用。
    猜你喜欢
    • 1970-01-01
    • 2019-08-30
    • 2017-09-04
    • 2022-06-10
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-09
    相关资源
    最近更新 更多