Selenium ChromeDriver 身份验证代理

7
我需要使用chromedriver webdriver在selenium 2中连接到代理服务器,并输入用户名和密码(即USERNAME:PASSWD@IP:PORT)。我已经找到了如何在没有使用用户名和密码的情况下连接到代理服务器,但还没有找到使用它们的方法。
谢谢。
1个回答

0

使用工具sshuttle在您的计算机上运行是连接带有用户名和密码的代理服务器的一种方法。此工具允许您使用SSH创建到代理服务器的连接,然后路由所有流量。

$ sshuttle -r username@remotehost 0.0.0.0/0

请参阅https://sshuttle.readthedocs.io/en/stable/usage.html以获取更多信息。

另一种方法是添加Chrome扩展程序。这种方法稍微复杂一些,但它允许您使用Chrome WebDriver而无需在后台运行任何工具。您需要通过将两个文件Background.jsmanifest.js包含在名为proxy.zip的存档中来创建Chrome扩展程序。

Background.js:

var config = {
    mode: "fixed_servers",
    rules: {
        singleProxy: {
            scheme: "http",
            host: "YOUR_PROXY_ADDRESS",
            port: parseInt(PROXY_PORT)
        },
        bypassList: ["foobar.com"]
    }
};
chrome.proxy.settings.set({ value: config, scope: "regular" }, function () { });
function callbackFn(details) {
    return {
        authCredentials: {
            username: "PROXY_USERNAME",
            password: "PROXY_PASSWORD"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
    callbackFn,
    { urls: ["<all_urls>"] },
    ['blocking']
);

manifest.js:

var config = {
    mode: "fixed_servers",
    rules: {
        singleProxy: {
            scheme: "http",
            host: "YOUR_PROXY_ADDRESS",
            port: parseInt(PROXY_PORT)
        },
        bypassList: ["foobar.com"]
    }
};
chrome.proxy.settings.set({ value: config, scope: "regular" }, function () { });
function callbackFn(details) {
    return {
        authCredentials: {
            username: "PROXY_USERNAME",
            password: "PROXY_PASSWORD"
        }{
            "version": "1.0.0",
            "manifest_version": 3,
            "name": "Chrome Proxy",
            "permissions": [
            "Proxy",
            "Tabs",
            "unlimitedStorage",
            "Storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
            ],
            "background": {
            "scripts": ["background.js"]
            },
            "Minimum_chrome_version":"76.0.0"
            }
    };
}

chrome.webRequest.onAuthRequired.addListener(
    callbackFn,
    { urls: ["<all_urls>"] },
    ['blocking']
);

然后您需要使用addExtension方法将Chrome扩展程序添加到ChromeOptions中:

        ChromeOptions options = new ChromeOptions();
        options.addExtensions("proxy.zip");

有关Python的详细信息,请参阅页面:https://www.browserstack.com/guide/set-proxy-in-selenium


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接