C# Selenium Firefox 使用 Auth 设置 socks 代理
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 配置文件 link。然后很容易完成这项工作。我使用该代码:
FirefoxProfile profile = new FirefoxProfile(pathToProfile);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(options);
那么您将不得不抑制用户名和密码验证的警报。您可以使用两种方法来做到这一点。第一个是以编程方式进行的。类似的东西:
var alert = driver.SwitchTo().Alert();
alert.Accept();
另一种方法是从 Firefox 配置文件设置中进行。
经过大量研究,这是我决定采用的解决方案。
这是一个肮脏的技巧,但它确实有效。
我使用 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
{
}
}
在 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 配置文件 link。然后很容易完成这项工作。我使用该代码:
FirefoxProfile profile = new FirefoxProfile(pathToProfile);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
driver = new FirefoxDriver(options);
那么您将不得不抑制用户名和密码验证的警报。您可以使用两种方法来做到这一点。第一个是以编程方式进行的。类似的东西:
var alert = driver.SwitchTo().Alert();
alert.Accept();
另一种方法是从 Firefox 配置文件设置中进行。
经过大量研究,这是我决定采用的解决方案。
这是一个肮脏的技巧,但它确实有效。
我使用 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
{
}
}