[英]Delphi/C#: WebBrowser socks5 proxy (with username/password auth)
[英]C# Selenium Firefox Set socks proxy with Auth
在 C# 中,我试图在 firefox 中设置带有身份验证的 socks 代理。
这不起作用
Proxy proxy = new Proxy();
proxy.SocksProxy = sProxyIP + ":" + sProxyPort;
proxy.SocksUserName = sProxyUser;
proxy.SocksPassword = sProxyPass;
options.Proxy = proxy;
_driver = new FirefoxDriver(service, options);
这也行不通
profile.SetPreference("network.proxy.socks", sProxyUser + ":" + sProxyPass + "@" + sProxyIP + ":" + sProxyPort);
profile.SetPreference("network.proxy.socks_port", sProxyPort);
我该如何解决这个问题?
据我所知,你不能用 Firefox 那样做。 您需要添加一个新的 Firefox 配置文件,然后与 Selenium 一起使用。 在此配置文件中,您需要保存代理信息并保存用户名和密码。
您可以按照此步骤链接设置 Firefox 配置文件。 然后很容易完成这项工作。 我使用该代码:
FirefoxProfile profile = new FirefoxProfile(pathToProfile);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(options);
然后,您将不得不取消用户名和密码验证的警报。 您可以使用两种方法来做到这一点。 第一个是以编程方式进行的。 像这样的东西:
var alert = driver.SwitchTo().Alert();
alert.Accept();
另一种方法是从 Firefox 配置文件设置中执行此操作。
经过大量研究,这里是我决定 go 的解决方案。
这是一个肮脏的黑客,但它有效。
我使用AutoitX
来自动化代理身份验证 window 但必须使用System.Windows.Automation
才能获得正确的身份验证 window 因为我的应用程序将是多线程的。
sProxyIP = "154.5.5.5";
sProxyUser = "user here";
sProxyPass = "pass here";
sProxyPort = 4444;
//Set proxy
profile.SetPreference("network.proxy.socks", sProxyIP);
profile.SetPreference("network.proxy.socks_port", sProxyPort);
//deal with proxy auth
_driver.Manage().Timeouts().PageLoad = TimeSpan.FromMilliseconds(0);
WebsiteOpen(@"https://somewebsite.com/");
AuthInProxyWindow(sProxyUser, sProxyPass);
_driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(60);
void ProxyAuthWindow(string login, string pass)
{
try
{
//wait for the auth window
var sHwnd = AutoItX.WinWait("Authentication Required", "", 2);
AutoItX.WinSetOnTop("Authentication Required", "", 1);
//we are using Windows UIA so we make sure we got the right auth
//dialog(since there will be multiple threads we can easily hit the wrong one)
var proxyWindow = AutomationElement.RootElement.FindFirst(TreeScope.Subtree,
new PropertyCondition(AutomationElement.ClassNameProperty, "MozillaDialogClass"));
string hwnd = "[handle:" + proxyWindow.Current.NativeWindowHandle.ToString("X") + "]";
AutoItX.ControlSend(hwnd, "", "", login, 1);
AutoItX.ControlSend(hwnd, "", "", "{TAB}", 0);
AutoItX.ControlSend(hwnd, "", "", pass, 1);
AutoItX.ControlSend(hwnd, "", "", "{ENTER}", 0);
}
catch
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.