我需要使用用户名和密码(即USERNAME:PASSWD@IP:端口)在java中使用selenium 2中的chromedriver网络驱动程序。我发现了如何在不使用用户名的情况下做到这一点和密码,但还没有找到使用的方法。
谢谢。
使用用户名和密码连接到代理服务器的一种方法是使用机器上运行的工具sshuttle
。该工具允许您使用SSH创建到代理服务器的连接,然后路由所有流量。
$ sshuttle -r username@remotehost 0.0.0.0/0
请参阅https://sshuttle.readthedocs.io/en/stable/usage.html以获取更多信息。
另一种方法是添加Chrome扩展。这种方法有点复杂,但它允许您在没有任何工具在后台运行的情况下使用Chrome WebDriver。您需要通过在名为proxy.zip
的归档中包含两个文件来创建Chrome扩展:Background.js
和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']
);
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