如果您的代理服务器需要身份验证,则在下面的代码中设置代理服务器参数,然后FireFox将带来身份验证对话框,基本上您无法自动填写它。 那么无论如何设置 USERNAME 和 PASSWORD ?
FirefoxProfile profile = new FirefoxProfile();
String PROXY = "192.168.1.100:8080";
OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy=PROXY;
proxy.FtpProxy=PROXY;
proxy.SslProxy=PROXY;
profile.SetProxyPreferences(proxy);
FirefoxDriver driver = new FirefoxDriver(profile);
如果您尝试将代理字符串格式化为http://username:pass@192.168.1.1:8080
之类的内容
您收到字符串无效的错误。所以我想知道必须有办法实现这一点。
任何帮助都将不胜感激。
答案 0 :(得分:2)
String PROXY = "http://login:pass@proxy:port";
ChromeOptions options = new ChromeOptions();
options.AddArguments("user-data-dir=path/in/your/system");
Proxy proxy = new Proxy();
proxy.HttpProxy = PROXY;
proxy.SslProxy = PROXY;
proxy.FtpProxy = PROXY;
options.Proxy = proxy;
// Initialize the Chrome Driver
using (var driver = new ChromeDriver(options))
答案 1 :(得分:0)
您可以做的是创建配置文件并将身份验证数据保存在其中。 如果您的配置文件名为“webdriver”,则可以在初始化中从代码中选择它:
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
profile.setPreferences("foo.bar",23);
WebDriver driver = new FirefoxDriver(profile);
答案 2 :(得分:0)
没有AutoIt的MS UI自动化:
public void AuthInProxyWindow (string login, string pass)
{
var proxyWindow = AutomationElement.RootElement
.FindFirst(TreeScope.Subtree,
new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaDialogClass"));
var edits = proxyWindow.FindAll(TreeScope.Subtree,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
var unamePoint = edits[1].GetClickablePoint();
Mouse.MoveTo(new Point((int) unamePoint.X, (int) unamePoint.Y));
Mouse.Click(MouseButton.Left);
SendKeys.SendWait(login);
var pwdPoint = edits[2].GetClickablePoint();
Mouse.MoveTo(new Point((int) pwdPoint.X, (int) pwdPoint.Y));
Mouse.Click(MouseButton.Left);
SendKeys.SendWait(pass);
Keyboard.Press(Key.Return);
Logger.Debug("Authefication in Firefox completed succesfully");
}
答案 3 :(得分:0)
要阻止firefox向您提供简单的身份验证弹出窗口,请确保在设置阶段将代理URL设置为包括身份验证详细信息,如下所示:
var myProxy = user + ":" + pass + "@" + proxyIP + ":" + proxyPORT;
options.SetPreference("network.proxy.type", 1);
options.SetPreference("network.proxy.http", myProxy);
options.SetPreference("network.proxy.http_port", proxyPORT);
options.SetPreference("general.useragent.override", useragent);
driver = new FirefoxDriver(driverService, options);
答案 4 :(得分:0)
您可以编写自己的用于代理的firefox扩展,然后从selenium启动。您需要写入2个文件并将其打包。
background.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>"]});
manifest.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模式下的zip归档文件,并以 .xpi 结尾,例如 my_proxy_extension.xpi 。
您有两种选择:
签署您的扩展程序Here you can read more about verify extension and extension's structure
OR
无符号运行。对于此步骤:
about:config
打开firefox标志并将 xpinstall.signatures.required 选项设置为 false OR
在以下位置更新firefox配置文件:
Windows :C:\ Program Files \ Mozilla Firefox \ defaults \ pref \ channel-prefs.js
Linux :/etc/firefox/syspref.js
在文件末尾添加下一行:
pref(“ xpinstall.signatures.required”,false);
执行此步骤后,运行selenium并安装此扩展程序:
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(new File("path/to/my_proxy_extension.xpi"));
driver = new FirefoxDriver(profile);