selenium chromedriver认证代理

Nim*_*mon 6 java proxy selenium-webdriver

我需要在java中的selenium 2中使用chromedriver webdriver连接到具有用户名和密码的代理服务器(即USERNAME:PASSWD @ IP:PORT).我已经找到了如何在不使用用户名和密码的情况下完成它,但是还没有找到办法.

谢谢.

Dan*_*ata 0

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

$ sshuttle -r username@remotehost 0.0.0.0/0
Run Code Online (Sandbox Code Playgroud)

请参阅https://sshuttle.readthedocs.io/en/stable/usage.html了解更多信息。

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

背景.js

$ sshuttle -r username@remotehost 0.0.0.0/0
Run Code Online (Sandbox Code Playgroud)

清单.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']
);
Run Code Online (Sandbox Code Playgroud)

然后您需要使用以下addExtension方法将 Chrome 扩展添加到 ChromeOptions:

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']
);
Run Code Online (Sandbox Code Playgroud)

页面上描述了详细信息(针对 Python): https: //www.browserstack.com/guide/set-proxy-in-selenium