简体   繁体   English

使用 Chrome 驱动程序进行 C# Selenium 代理身份验证

[英]C# Selenium Proxy Authentication with Chrome Driver

I am using the following code for the proxy.我正在为代理使用以下代码。 however, when chrome starts, pop-up window will pop up and the program will be locked.但是,当 chrome 启动时,会弹出弹出窗口并锁定程序。

public async void StartDriver(string proxy)
    {
        var proxys = new Proxy();
        ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
        chromeDriverService.HideCommandPromptWindow = true;
        ChromeOptions chromeOptions = new ChromeOptions();
        bool flag = !string.IsNullOrEmpty(proxy);
        if (flag)
        {
            proxys.Kind = ProxyKind.Manual;
            proxys.IsAutoDetect = false;
            proxys.SslProxy = proxy;
            chromeOptions.Proxy = proxys;
        }
        driver = new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromMinutes(10));
        await Task.Delay(2000);
    }

I tried http or ssl the same...我尝试过同样的 http 或 ssl ...

StartDriver("88.55.66.77:8080");

Or或者

StartDriver("http://username:pass@88.55.66.77:8080");

I could not start a browser with a kind of proxy.我无法使用某种代理启动浏览器。

I want a code that automatically enters the username and password.我想要一个自动输入用户名和密码的代码。 I don't want autoitx3.dll.我不想要 autoitx3.dll。

is there a way to start a secure proxy?有没有办法启动安全代理?

Thank you.谢谢你。

is there a way to start a secure proxy? 有没有办法启动安全代理?

There's one. 有一个 You need to create a chrome extension with proxy settings. 您需要使用代理设置创建chrome扩展程序。

manifest.json 的manifest.json

    {
        "version": "0.1.0",
        "manifest_version": 2,
        "name": "%NAME IT AS YOU WANT%",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }

background.js background.js

//note that it's a JS code. You can use any additional code to do anything :) 
var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: "%HOST%",
        port: parseInt(%PORT%)
      },
      bypassList: ["foobar.com"]
    }
  };

chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

function callbackFn(details) {
    return {
        authCredentials: {
            username: "%USERNAME%",
            password: "%PASSWORD%"
        }
    };
}

chrome.webRequest.onAuthRequired.addListener(
        callbackFn,
        {urls: ["<all_urls>"]},
        ['blocking']
);

Pack it as an archive. 将其打包为档案。 For instance yourExt.dat 例如yourExt.dat

var proxy = "yourExt.dat";
var options = new ChromeOptions();
options.AddExtension(proxy);
var driver = new ChromeDriver(options);

In 2021 you can use Selenium 4.0 and BiDi APIs for making requests through a proxy with authorization. 2021 年,您可以使用 Selenium 4.0 和BiDi API通过具有授权的代理发出请求。 Example:例子:

var options = new ChromeOptions {AcceptInsecureCertificates = true};
options.AddArgument("headless");
options.Proxy = new Proxy {HttpProxy = "1.1.1.1:12345", SslProxy = "1.1.1.1:12345", Kind = ProxyKind.Manual};
//options.AddArguments($"--proxy-server=http://1.1.1.1:12345");
var driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromMinutes(2));

NetworkAuthenticationHandler handler = new NetworkAuthenticationHandler()
{
    UriMatcher = d => true, //d.Host.Contains("your-host.com")
    Credentials = new PasswordCredentials("proxy_user", "proxy_pass")
};

var networkInterceptor = driver.Manage().Network;
networkInterceptor.AddAuthenticationHandler(handler);
networkInterceptor.StartMonitoring();

_driver.Navigate().GoToUrl("https://stackoverflow.com/");

networkInterceptor.StopMonitoring();
driver.Quit();

Selenium 4 has a bug. Selenium 4 有一个错误。 WebSocket exception during performing basic authorization https://github.com/SeleniumHQ/selenium/issues/10054执行基本授权期间的 WebSocket 异常https://github.com/SeleniumHQ/selenium/issues/10054

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM