使用用户名和密码连接到代理服务器的一种方法是使用该工具sshuttle
在您的机器上运行。该工具允许您使用 SSH 创建到代理服务器的连接,然后路由所有流量。
$ sshuttle -r username@remotehost 0.0.0.0/0
See https://sshuttle.readthedocs.io/en/stable/usage.html https://sshuttle.readthedocs.io/en/stable/usage.html了解更多信息。
另一种方法是添加 Chrome 扩展程序。这种方法有点复杂,但它允许您使用 Chrome WebDriver,而无需在后台运行任何工具。您需要通过在名为的存档中包含两个文件来创建 Chrome 扩展程序proxy.zip
: Background.js
and manifest.js
.
背景.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']
);
清单.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']
);
然后您需要使用以下命令将 Chrome 扩展添加到 ChromeOptionsaddExtension
method:
ChromeOptions options = new ChromeOptions();
options.addExtensions("proxy.zip");
页面上描述了详细信息(对于 Python):https://www.browserstack.com/guide/set-proxy-in-selenium https://www.browserstack.com/guide/set-proxy-in-selenium