├── .gitignore ├── ConsoleApp ├── ConsoleApp.csproj └── Program.cs ├── OpenQA.Selenium.Chrome.Fiddler ├── OpenQA.Selenium.Chrome.Fiddler.csproj └── ChromeOptionsExtensions.cs ├── README.md └── OpenQA.Selenium.Chrome.Fiddler.sln /.gitignore: -------------------------------------------------------------------------------- 1 | _ReSharper.Caches/ 2 | .vs/ 3 | ConsoleApp/bin/ 4 | ConsoleApp/obj/ 5 | OpenQA.Selenium.Chrome.Fiddler/obj/ 6 | OpenQA.Selenium.Chrome.Fiddler/bin/ 7 | -------------------------------------------------------------------------------- /ConsoleApp/ConsoleApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /OpenQA.Selenium.Chrome.Fiddler/OpenQA.Selenium.Chrome.Fiddler.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | enable 6 | yuzd 7 | yuzd 8 | OpenQA.Selenium.Chrome 9 | true 10 | This extensions allows you to do something fun like fiddler to Selenium.ChromeDriver 11 | MIT 12 | https://github.com/yuzd 13 | Selenium, Chrome, ChromeDriver, Fiddler, HttpProxy 14 | 1.0.0.0 15 | 1.0.0.0 16 | 1.0.0 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenQA.Selenium.Chrome.Fiddler 2 | Extensions for ChromeOptions (Selenium Driver)支持配置拦截或转发指定请求(正则) 3 | 4 | # nuget 5 | OpenQA.Selenium.Chrome.Fiddler 6 | 7 | # how to use 8 | 9 | ```csharp 10 | 11 | options.AddFiddler(new FiddlerOption 12 | { 13 | OnBeforeRequestOptions = new List 14 | { 15 | // 配置转发 16 | new FiddlerOnBeforeRequestOptions 17 | { 18 | Match = "https://www.cnblogs.com/yudongdong/ajax/GetPostStat",//正则 19 | RedirectUrl = "http://localhost:5000/GetPostStat",//如果匹配成功则将requestBody转发到这个url中去 20 | Cancel = false//如果配置了cancel=true那么转发将无效,true的意思是直接拦截这次的请求,不去发送了 21 | }, 22 | // 配置拦截 23 | new FiddlerOnBeforeRequestOptions 24 | { 25 | Match = "https://www.cnblogs.com/yudongdong/ajax/blogStats", 26 | Cancel = true//true的意思是直接拦截这次的请求,不去发送了 27 | }, 28 | } 29 | }); 30 | 31 | 32 | ``` 33 | 34 | ![image](https://dimg04.c-ctrip.com/images/0v52p120009obg6zn40D0.gif) 35 | -------------------------------------------------------------------------------- /ConsoleApp/Program.cs: -------------------------------------------------------------------------------- 1 | using OpenQA.Selenium; 2 | using OpenQA.Selenium.Chrome; 3 | using OpenQA.Selenium.Chrome.ChromeDriverExtensions; 4 | using OpenQA.Selenium.Chrome.Fiddler; 5 | using RestSharp; 6 | 7 | var driverBinary = @"D:\soft\chrome\chrome2\Chrome-bin\"; 8 | 9 | ChromeOptions options = new ChromeOptions 10 | { 11 | BinaryLocation = Path.Combine(driverBinary, "chrome.exe") 12 | }; 13 | 14 | Environment.SetEnvironmentVariable("webdriver.chrome.driver", driverBinary); 15 | options.AddArgument("--disable-blink-features=AutomationControlled"); 16 | options.AddArguments("--disable-infobars"); 17 | List ls = new List { "enable-automation" }; 18 | options.AddExcludedArguments(ls); 19 | 20 | #region 代理 21 | 22 | // var pp = GetProxy().Split(":"); 23 | // options.AddHttpProxy(pp[0], int.Parse(pp[1]), "", ""); 24 | 25 | #endregion 26 | 27 | #region Fillder 28 | 29 | options.AddFiddler(new FiddlerOption 30 | { 31 | OnBeforeRequestOptions = new List 32 | { 33 | new FiddlerOnBeforeRequestOptions 34 | { 35 | Match = "https://www.cnblogs.com/yudongdong/ajax/GetPostStat", 36 | RedirectUrl = "http://localhost:5000/GetPostStat", 37 | Cancel = false 38 | } 39 | } 40 | }); 41 | 42 | #endregion 43 | 44 | var chrome = new ChromeDriver(driverBinary, options); 45 | 46 | 47 | #region method 48 | 49 | string GetProxy() 50 | { 51 | var client = new RestClient("http://webapi.http.zhimacangku.com/getip?num=1&type=1&pro=&city=0&yys=0&port=1&time=1&ts=0&ys=0&cs=0&lb=1&sb=0&pb=4&mr=1®ions="); 52 | var request = new RestRequest(); 53 | var response = client.Execute(request); 54 | return response.Content; 55 | } 56 | 57 | #endregion 58 | -------------------------------------------------------------------------------- /OpenQA.Selenium.Chrome.Fiddler.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32328.378 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenQA.Selenium.Chrome.Fiddler", "OpenQA.Selenium.Chrome.Fiddler\OpenQA.Selenium.Chrome.Fiddler.csproj", "{85F0B777-0959-44EC-BF9C-E157AFBCF179}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{C6D1C06E-9197-4284-ACD3-B80DA58A682C}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {85F0B777-0959-44EC-BF9C-E157AFBCF179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {85F0B777-0959-44EC-BF9C-E157AFBCF179}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {85F0B777-0959-44EC-BF9C-E157AFBCF179}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {85F0B777-0959-44EC-BF9C-E157AFBCF179}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {C6D1C06E-9197-4284-ACD3-B80DA58A682C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {C6D1C06E-9197-4284-ACD3-B80DA58A682C}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {C6D1C06E-9197-4284-ACD3-B80DA58A682C}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {C6D1C06E-9197-4284-ACD3-B80DA58A682C}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {3940B00E-549B-47E1-8C81-A2A53945146D} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /OpenQA.Selenium.Chrome.Fiddler/ChromeOptionsExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.IO.Compression; 5 | 6 | namespace OpenQA.Selenium.Chrome.Fiddler 7 | { 8 | /// 9 | /// Extensions for ChromeOptions (Selenium Driver) 10 | /// 支持配置拦截指定请求(正则) 11 | /// donothing 12 | /// 拦截后上传(http) 13 | /// 14 | public static class ChromeOptionsExtensions 15 | { 16 | private const string background_js = @" 17 | 18 | 19 | var before_configs = {before_configs}; 20 | 21 | chrome.webRequest.onBeforeRequest.addListener( 22 | function(details) { 23 | for (let i = 0; i']}, 37 | ['blocking', 'extraHeaders', 'requestBody'] 38 | ); 39 | "; 40 | 41 | private const string manifest_json = @" 42 | { 43 | ""version"": ""1.0.0"", 44 | ""manifest_version"": 2, 45 | ""name"": ""Chrome Fiddler"", 46 | ""permissions"": [ 47 | ""proxy"", 48 | ""tabs"", 49 | ""unlimitedStorage"", 50 | ""storage"", 51 | """", 52 | ""webRequest"", 53 | ""webRequestBlocking"" 54 | ], 55 | ""background"": { 56 | ""scripts"": [""background.js""] 57 | }, 58 | ""minimum_chrome_version"":""22.0.0"" 59 | }"; 60 | 61 | /// 62 | /// Add Fiddler extention 63 | /// 64 | /// Chrome options 65 | /// Proxy host 66 | public static void AddFiddler(this ChromeOptions options, FiddlerOption fiddlerOption) 67 | { 68 | var backgroundProxyJs = ReplaceTemplates(background_js, fiddlerOption); 69 | 70 | if (!Directory.Exists("Plugins")) 71 | Directory.CreateDirectory("Plugins"); 72 | 73 | var guid = Guid.NewGuid().ToString(); 74 | 75 | var manifestPath = $"Plugins/manifest_{guid}.json"; 76 | var backgroundPath = $"Plugins/background_{guid}.js"; 77 | var archiveFilePath = $"Plugins/proxy_auth_plugin_{guid}.zip"; 78 | 79 | File.WriteAllText(manifestPath, manifest_json); 80 | File.WriteAllText(backgroundPath, backgroundProxyJs); 81 | 82 | using (var zip = ZipFile.Open(archiveFilePath, ZipArchiveMode.Create)) 83 | { 84 | zip.CreateEntryFromFile(manifestPath, "manifest.json"); 85 | zip.CreateEntryFromFile(backgroundPath, "background.js"); 86 | } 87 | 88 | File.Delete(manifestPath); 89 | File.Delete(backgroundPath); 90 | 91 | options.AddExtension(archiveFilePath); 92 | } 93 | 94 | private static string ReplaceTemplates(string str, FiddlerOption fiddlerOption) 95 | { 96 | if (fiddlerOption.OnBeforeRequestOptions != null) 97 | { 98 | var beforeConfigs = Newtonsoft.Json.JsonConvert.SerializeObject(fiddlerOption.OnBeforeRequestOptions); 99 | str = str.Replace("{before_configs}", beforeConfigs); 100 | } 101 | return str; 102 | } 103 | 104 | 105 | } 106 | 107 | public sealed class FiddlerOption 108 | { 109 | public List? OnBeforeRequestOptions { get; set; } 110 | } 111 | 112 | public sealed class FiddlerOnBeforeRequestOptions 113 | { 114 | public string Match { get; set; } = ""; 115 | public bool Cancel { get; set; } 116 | public string RedirectUrl { get; set; } = ""; 117 | } 118 | } 119 | --------------------------------------------------------------------------------