【问题标题】:Proxy server for Selenium and PythonSelenium 和 Python 的代理服务器
【发布时间】:2016-03-14 21:41:23
【问题描述】:

我正在尝试使用代理服务器来启动 Selenium Chrome 驱动程序。到目前为止,我找到的唯一解决方案是使用一种用于 Chrome 的插件进行身份验证,但它不是很可靠,所以我想知道是否还有其他选择。

这是我现在使用的

    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: "",
    port: parseInt(6060)
    },
    bypassList: ["foobar.com"]
    }
    };

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

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

    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(executable_path='/usr/local/bin/chromedriver')

【问题讨论】:

    标签: python selenium proxy


    【解决方案1】:

    我不认为插件是一个很好的解决方案,当有在所有浏览器、版本和操作系统上都一样工作的纯 HTTP 解决方案时肯定不是。

    您可以将此Python wrapper 用于Browsermob,这是一个非常著名的用Java 编写的独立或嵌入式代理服务器。

    code 展示了如何使用 Python 方法 headers() 添加标头,而无需 POST 到 REST API。测试中有 some examples 正在使用中,例如:

    self.client = Client("localhost:9090")
    self.client.headers({'User-Agent': 'rubber ducks floating in a row'})
    self.client.headers({'Authorization': 'Basic dXNlcjpwYXNz'})  # User 'user', password 'pass'
    

    更新:

    只是为了阐明 WebDriver 和代理如何组合在一起:

    • 首先启动您的代理服务器,然后等待它准备好。您可以在外部执行此操作并将 host:port 传递给 WebDriver,或者将其嵌入到您的应用程序中,然后将 proxy 对象传递给 WebDriver。
    • This example 演示了第二种方法,使用 Firefox 配置文件和 Chrome 选项。
    • 或者,启动嵌入的代理,使用它获取代表 Selenium 代理服务器的Proxy 对象,然后将其添加到您的DesiredCapabilities 对象中,并按照this example 以这种方式创建您的驱动程序。

    从那时起,代理侦听是自动的,您可以开始创建您的 HAR 文件。


    或者,您可以看到 this answer 用于使用 Twisted 的自定义 Python 代理服务器。

    我对 Python here 中的 Selenium 代理有更长的回答。

    【讨论】:

    • 抱歉,我错过了链接。现在在答案中修复。 github.com/AutomatedTester/browsermob-proxy-py
    • 再次感谢。所以这是 Chrome 的代码,例如来自browsermobproxy import Server server = Server("path/to/browsermob-proxy") server.start() proxy = server.create_proxy() chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) browser = webdriver.Chrome(chrome_options = chrome_options)
    • 它是否也允许您使用用户名和密码?谢谢
    • 只有时间快速回答,但如果您的意思是使用用户/通行证进行基本身份验证对话框,是的。我在这里介绍:stackoverflow.com/q/35004026/954442
    • 谢谢。现在试图弄清楚如何在 Python 中实现该 POST 调用。
    猜你喜欢
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2017-03-09
    • 2011-08-22
    相关资源
    最近更新 更多