您可以为代理编写自己的 firefox 扩展,并从 selenium 启动。您需要编写 2 个文件并将其打包。
背景.js
var proxy_host = "YOUR_PROXY_HOST";
var proxy_port = YOUR_PROXY_PORT;
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: proxy_host,
port: proxy_port
},
bypassList: []
}
};
function proxyRequest(request_data) {
return {
type: "http",
host: proxy_host,
port: proxy_port
};
}
browser.proxy.settings.set({value: config, scope: "regular"}, function() {;});
function callbackFn(details) {
return {
authCredentials: {
username: "YOUR_USERNAME",
password: "YOUR_PASSWORD"
}
};
}
browser.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
browser.proxy.onRequest.addListener(proxyRequest, {urls: ["<all_urls>"]});
清单.json
{
"name": "My Firefox Proxy",
"version": "1.0.0b",
"manifest_version": 2,
"permissions": [
"browsingData",
"proxy",
"storage",
"tabs",
"webRequest",
"webRequestBlocking",
"downloads",
"notifications",
"<all_urls>"
],
"background": {
"scripts": ["background.js"]
},
"browser_specific_settings": {
"gecko": {
"id": "myproxy@example.org"
}
}
}
接下来,您需要将这些文件打包到 DEFLATED 模式下以.xpi结尾的压缩存档,如my_proxy_extension.xpi。
你有两个选择:
签署你的扩展 在这里你可以阅读更多关于验证扩展和扩展结构的信息
或者
运行无符号。对于这一步:
- 打开 Firefox 标志
about:config
并将选项xpinstall.signatures.required设置为false
或者
将下一行添加到文件末尾:
pref("xpinstall.signatures.required",false);
在此步骤之后运行 selenium 并安装此扩展:
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("path/to/my_proxy_extension.xpi"));
driver = new FirefoxDriver(profile);