最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to do Proxy Authentication in Selenium using Undetected Chrome Driver in C#? - Stack Overflow

programmeradmin0浏览0评论

I was trying to set up proxy with credentials in Selenium using Undetected Chrome Driver in C#. I have also tried to load the extenstion without the Zip file but still I did not get any success. I am trying to set the proxy like this :-

static string CreateProxyAuthExtension(string proxyHost, int proxyPort, string proxyUser, string proxyPass)
{
    string manifestJson = @"
    {
        ""manifest_version"": 2,
        ""name"": ""Proxy Auth Extension"",
        ""version"": ""1.0"",
        ""permissions"": [
            ""proxy"",
            ""tabs"",
            ""unlimitedStorage"",
            ""storage"",
            ""<all_urls>"",
            ""webRequest"",
            ""webRequestBlocking""
        ],
        ""background"": {
            ""scripts"": [""background.js""]
        },
        ""minimum_chrome_version"": ""22""
    }";

    string backgroundJs = @"
                chrome.runtime.onInstalled.addListener(() => {
                chrome.storage.local.set({
                        proxyHost: '" + proxyHost + @"',
                        proxyPort: '" + proxyPort + @"',
                        proxyUser: '" + proxyUser + @"',
                        proxyPass: '" + proxyPass + @"'
                    });
                    chrome.storage.local.get(['proxyHost', 'proxyPort', 'proxyUser', 'proxyPass'], function (data) {
                        var config = {
                            mode: 'fixed_servers',
                            rules: {
                                singleProxy: {
                                    scheme: 'http',
                                    host: data.proxyHost || 'default.proxy.server',
                                    port: parseInt(data.proxyPort) || 8080
                                },
                                bypassList: ['localhost', '127.0.0.1']
                            }
                        };

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

                        chrome.webRequest.onAuthRequired.addListener(
                            function (details) {
                                return {
                                    authCredentials: {
                                        username: data.proxyUser || 'defaultUser',
                                        password: data.proxyPass || 'defaultPass'
                                    }
                                };
                            },
                            { urls: ['<all_urls>'] },
                            ['blocking']
                        );
                    });
                });
                ";


    string extensionDir = Path.Combine(Directory.GetCurrentDirectory(), "extension");
    string extensionZip = Path.Combine(Directory.GetCurrentDirectory(), "proxy_auth_extension.zip");  //Path.Combine(extensionDir, "proxy_auth_extension.zip");
    try
    {
        // Create extension folder
        if (!Directory.Exists(extensionDir))
            Directory.CreateDirectory(extensionDir);

        // Write files
        File.WriteAllText(Path.Combine(extensionDir, "manifest.json"), manifestJson);
        File.WriteAllText(Path.Combine(extensionDir, "background.js"), backgroundJs);

        //Create ZIP archive for the extension
        if (File.Exists(extensionZip))
                File.Delete(extensionZip);
        ZipFile.CreateFromDirectory(extensionDir, "proxy_auth_extension.zip");
    }

    catch (Exception ex)
    {

    }
    return extensionZip;
}

ChromeOptions m_Options = new ChromeOptions();
        extensionPath = CreateProxyAuthExtension(objWebProxy.Address.Host, objWebProxy.Address.Port, ((System.Net.NetworkCredential)objWebProxy.Credentials).UserName, ((System.Net.NetworkCredential)objWebProxy.Credentials).Password);
        m_Options.AddExtension(extensionPath);

m_Options.AddArgument("--disable-blink-features=AutomationControlled");

    using (var driver = UndetectedChromeDriver.Create(options: m_Options, driverExecutablePath: new ChromeDriverInstaller().Auto().Result))
    {
        driver.Manage().Window.Maximize();
    
        strCompleteURL = "/";
        driver.Navigate().GoToUrl(strCompleteURL);
    
    }

But proxy is not getting set with this code. I have searched all over google for the solution but found nothing. Can anybody help me in the right direction ?

发布评论

评论列表(0)

  1. 暂无评论