在Selenium + Python3 + Chrome WebDriver + Docker上配置代理

时间:2019-03-17 16:08:06

标签: selenium docker selenium-chromedriver python-3.7

我无法在Python 3.7上使用Selenium Chrome WebDriver使用代理进行连接

1-使用代理时(在chrome_options上添加-proxy-server =%s)

def selenium_connect():
    PROXY = "66.97.38.58:80"
    url = "http://whatsmyip.org"
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    chrome_options.add_argument('--proxy-server=%s' % PROXY)
    driver = webdriver.Chrome(chrome_options=chrome_options)
    driver.get(url)

在这种情况下,我得到的结果为空。

2-我尝试了以下替代方法:How do you run headless chrome and a proxy using selenium in python?

def selenium_connect():
    PROXY = "66.97.38.58:80"
    url = "http://whatsmyip.org"
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')

    desired_caps = chrome_options.to_capabilities()
    prox = Proxy()
    prox.proxy_type = ProxyType.MANUAL
    prox.http_proxy = PROXY
    prox.add_to_capabilities(desired_caps)

    driver = webdriver.Chrome(chrome_options=chrome_options, desired_capabilities=desired_caps)
    driver.get(url)

在这种情况下,连接是通过本地ip而不是代理的ip路由的。我已经在docker这里上传了该函数:

https://github.com/gerimo/challenge

  

我正在使用标准的ubuntu Docker环境,硒3.141.0,铬铬驱动程序68.0

1 个答案:

答案 0 :(得分:1)

这不是最短的解决方案,但我认为这可能对某人有益。我设法在Docker容器中使用通过luminati代理连接到硒集线器的远程Webdriver:

<img src="https://picsum.photos/id/76/400/300" style="position: absolute;">
<canvas id="canvas" style="position: absolute;" width="400" height="300"></canvas>

一旦我们授权自己,就可以在代码中的某个地方使用它:

from selenium import webdriver
from zipfile import ZipFile

PROXY_HOST = 'servercountry-gb.zproxy.luminati.io'
PROXY_PORT = 22225
PROXY_USER = 'lum-customer-####-zone-####-country-gb-dns-remote'
PROXY_PASS = '#####'

chrome_options = webdriver.ChromeOptions()

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":"50.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)

pluginfile = 'proxy_auth_plugin.zip'

with ZipFile(pluginfile, 'w') as zp:
    zp.writestr('manifest.json', manifest_json)
    zp.writestr('background.js', background_js)
chrome_options.add_extension(pluginfile)

我遇到了一个奇怪的问题,我不能使用无头铬,但无头铬似乎很好。远程Webdriver在Docker容器中没有头了。 (python 3.7.4)