如何在C#Selenium Chrome驱动程序中认证(用户/密码)代理

How to authenticate (user/password) proxy in c# selenium chrome driver

我有一个HTTP / HTTPS代理,需要使用用户名和密码进行身份验证。我该如何使用C#硒铬webdriver?

1
2
3
4
5
6
7
8
9
string host = proxies[count].Split(':')[0];
int port = Convert.ToInt32(proxies[count].Split(':')[1]) + 1;

string prox = host +":" + port.ToString();

OpenQA.Selenium.Proxy proxy = new OpenQA.Selenium.Proxy();
proxy.HttpProxy = prox;
proxy.SslProxy = prox;
options.Proxy = proxy;

options是我分配给驱动程序的ChromeOptions类。


我发现唯一成功的方法是使用AutoIT(https://www.autoitscript.com/site/autoit)。

我为所有主要浏览器设置了我的代理身份验证设置,但这对于Chrome来说应该可以使用。
我是从记忆中在手机上写的。如果不起作用,请告诉我,我会纠正。

1
2
3
4
5
WinWait("data:, - Google Chrome","","10")
If WinExists("data:, - Google Chrome","")Then
WinActivate("data:, - Google Chrome")
Send("USERNAMEGOESHERE"{TAB}")
Send("
USERPASSWORDGOESHERE"{ENTER}")

使用AutoIT,将其创建为脚本,将其编译为exe,将其保存在某处,并使用以下(Java代码)引用文件:

1
Runtime.getRuntime().exec("C:\\\\users\\\\USERID\\\\desktop\\\\FILENAME.exe");

我发现最好在调用触发代理授权的URL之前先调用代理脚本。


2021更新

我创建了一个简单的库(nuget程序包),可以帮助您进行硒代理身份验证。您可以在Github存储库中检查源。当使用无头驱动程序时,它也适用。

用法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public void Test()
{  
    // Create a local proxy server
    var proxyServer = new SeleniumProxyAuth();

    // Don't await, have multiple drivers at once using the local proxy server
    TestSeleniumProxyServer(proxyServer, new ProxyAuth("123.123.123.123", 80,"prox-username1","proxy-password1"));
    TestSeleniumProxyServer(proxyServer, new ProxyAuth("11.22.33.44", 80,"prox-username2","proxy-password2"));
    TestSeleniumProxyServer(proxyServer, new ProxyAuth("111.222.222.111", 80,"prox-username3","proxy-password3"));

    while (true) { }
}

private async Task TestSeleniumProxyServer(SeleniumProxyAuth proxyServer, ProxyAuth auth)
{
    // Add a new local proxy server endpoint
    var localPort = proxyServer.AddEndpoint(auth);

    ChromeOptions options = new ChromeOptions();
    //options1.AddArguments("headless");

    // Configure the driver's proxy server to the local endpoint port
    options.AddArgument($"--proxy-server=127.0.0.1:{localPort}");

    // Optional
    var service = ChromeDriverService.CreateDefaultService();
    service.HideCommandPromptWindow = true;

    // Create the driver
    var driver = new ChromeDriver(service, options);

    // Test if the driver is working correctly
    driver.Navigate().GoToUrl("https://www.myip.com/");
    await Task.Delay(5000);
    driver.Navigate().GoToUrl("https://amibehindaproxy.com/");
    await Task.Delay(5000);

    // Dispose the driver
    driver.Dispose();
}


2019更新

经过多次不幸的尝试,最简单的解决方案是为Chrome创建扩展程序,如Mike在这篇文章中所述。

这听起来很怪异,但实际上确实很简单。


这是针对Firefox的,但最佳答案可能会有所帮助。

C#Selenium WebDriver FireFox配置文件-使用具有身份验证的代理

您可以做的是创建一个配置文件并将身份验证数据保存在其中。如果您的配置文件称为" webdriver",则可以在初始化时从代码中选择它:

1
2
3
4
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
profile.setPreferences("foo.bar",23);
WebDriver driver = new FirefoxDriver(profile);