selenium.chrome寫擴(kuò)展攔截或轉(zhuǎn)發(fā)請求功能
Selenium.WebDriver
Selenium WebDriver 是一組開源 API,用于自動測試 Web 應(yīng)用程序,利用它可以通過代碼來控制chrome瀏覽器!
有時(shí)候我們需要mock接口的返回,或者攔截和轉(zhuǎn)發(fā)請求,今天就來實(shí)現(xiàn)這個(gè)功能
代碼已開源: https://github.com/yuzd/OpenQA.Selenium.Chrome.Fiddler
nuget
OpenQA.Selenium.Chrome.Fiddler
開始coding
我們新創(chuàng)建一個(gè)功能:OpenQA.Selenium.Chrome.Fiddler
一個(gè)chrome擴(kuò)展 最起碼有2個(gè)文件
manifest.json
background.js
稍微解釋一下:
manifest.json 是來描述chrome擴(kuò)展的
{
"version": "1.0.0",
"manifest_version": 2,
"name": "Chrome Fiddler",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}background.js 是邏輯處理模塊
因?yàn)閿r截api 或者 轉(zhuǎn)發(fā) 需要用的chrome的api
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
//邏輯處理
},
{ urls: ['<all_urls>']},
['blocking', 'extraHeaders', 'requestBody']
);這個(gè)api的函數(shù) 接收的details參數(shù)
- details.url 是api的接口
函數(shù)的返回
- {cancel:true} 攔截請求
- {redirectUrl:''} 轉(zhuǎn)發(fā)到指定url
寫selenium.chrome插件
- 新建一個(gè)netstand工程,然后引用
Selenium.WebDriver
復(fù)制以下代碼
/// <summary>
/// Add Fiddler extention
/// </summary>
/// <param name="options">Chrome options</param>
/// <param name="fiddlerOption">Proxy host</param>
public static void AddFiddler(this ChromeOptions options, FiddlerOption fiddlerOption)
{
var backgroundProxyJs = ReplaceTemplates(background_js, fiddlerOption);
if (!Directory.Exists("Plugins"))
Directory.CreateDirectory("Plugins");
var guid = Guid.NewGuid().ToString();
var manifestPath = $"Plugins/manifest_{guid}.json";
var backgroundPath = $"Plugins/background_{guid}.js";
var archiveFilePath = $"Plugins/proxy_auth_plugin_{guid}.zip";
File.WriteAllText(manifestPath, manifest_json);
File.WriteAllText(backgroundPath, backgroundProxyJs);
using (var zip = ZipFile.Open(archiveFilePath, ZipArchiveMode.Create))
{
zip.CreateEntryFromFile(manifestPath, "manifest.json");
zip.CreateEntryFromFile(backgroundPath, "background.js");
}
File.Delete(manifestPath);
File.Delete(backgroundPath);
options.AddExtension(archiveFilePath);
}
private static string ReplaceTemplates(string str, FiddlerOption fiddlerOption)
{
if (fiddlerOption.OnBeforeRequestOptions != null)
{
var beforeConfigs = Newtonsoft.Json.JsonConvert.SerializeObject(fiddlerOption.OnBeforeRequestOptions);
str = str.Replace("{before_configs}", beforeConfigs);
}
return str;
}上面的代碼主要是創(chuàng)建一個(gè)chrome擴(kuò)展zip包
然后再selenium.chrome啟動的時(shí)候傳進(jìn)去這個(gè)zip包的地址
使用方法
var driverBinary = @"D:\soft\chrome\chrome2\Chrome-bin\";
ChromeOptions options = new ChromeOptions
{
BinaryLocation = Path.Combine(driverBinary, "chrome.exe")
};
Environment.SetEnvironmentVariable("webdriver.chrome.driver", driverBinary);
options.AddArgument("--disable-blink-features=AutomationControlled");
options.AddArguments("--disable-infobars");
List<string> ls = new List<string> { "enable-automation" };
options.AddExcludedArguments(ls);
#region Fillder
options.AddFiddler(new FiddlerOption
{
OnBeforeRequestOptions = new List<FiddlerOnBeforeRequestOptions>
{
// 配置轉(zhuǎn)發(fā)
new FiddlerOnBeforeRequestOptions
{
Match = "https://www.cnblogs.com/yudongdong/ajax/GetPostStat",//正則
RedirectUrl = "http://localhost:5000/GetPostStat",//如果匹配成功則將requestBody轉(zhuǎn)發(fā)到這個(gè)url中去
Cancel = false//如果配置了cancel=true那么轉(zhuǎn)發(fā)將無效,true的意思是直接攔截這次的請求,不去發(fā)送了
},
// 配置攔截
new FiddlerOnBeforeRequestOptions
{
Match = "https://www.cnblogs.com/yudongdong/ajax/blogStats",
Cancel = true//true的意思是直接攔截這次的請求,不去發(fā)送了
},
}
});
#endregion
var chrome = new ChromeDriver(driverBinary, options);實(shí)現(xiàn)效果

到此這篇關(guān)于selenium.chrome寫擴(kuò)展攔截或轉(zhuǎn)發(fā)請求的文章就介紹到這了,更多相關(guān)selenium chrome寫擴(kuò)展攔截或轉(zhuǎn)發(fā)請求內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#中FileSystemWatcher類實(shí)現(xiàn)監(jiān)控文件夾
在C#中,如果你想要監(jiān)控一個(gè)文件夾內(nèi)文件的變動情況,比如文件的創(chuàng)建、刪除、修改等,你可以使用FileSystemWatcher類,下面就來介紹一下FileSystemWatcher監(jiān)控的使用,感興趣的可以了解一下2024-03-03
C#進(jìn)行圖像處理的常見方法(Bitmap,BitmapData,IntPtr)使用詳解
這篇文章主要為大家詳細(xì)介紹了C#進(jìn)行圖像處理的幾個(gè)常見方法(Bitmap,BitmapData,IntPtr)具體使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01
C#開發(fā)WinForm之DataGridView開發(fā)詳解
這篇文章主要介紹了C#開發(fā)WinForm之DataGridView開發(fā)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法
這篇文章主要介紹了C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法,涉及C#操作URL的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
C#獲取真實(shí)IP地址(IP轉(zhuǎn)為長整形、判斷是否內(nèi)網(wǎng)IP的方法)
這篇文章主要介紹了C#獲取真實(shí)IP地址的實(shí)現(xiàn)代碼,包含把IP轉(zhuǎn)為長整形、判斷是否是私網(wǎng)、內(nèi)網(wǎng)IP的方法,需要的朋友可以參考下2014-08-08
C#實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及contextMenu自定義
ContextMenu 類表示當(dāng)用戶在控件或窗體的特定區(qū)域上單擊鼠標(biāo)右鍵時(shí)會顯示的快捷菜單,要想實(shí)現(xiàn)自定義的Menu和ContextMenu效果,大家可以通過派生ProfessionalColorTable類,下面小編把實(shí)現(xiàn)Menu和ContextMenu自定義風(fēng)格及ContextMenu自定義給大家整理一下2015-08-08
C#獲取進(jìn)程的主窗口句柄的實(shí)現(xiàn)方法
C#獲取進(jìn)程的主窗口句柄的實(shí)現(xiàn)方法,需要的朋友可以參考一下2013-04-04
在C#使用字典存儲事件示例及實(shí)現(xiàn)自定義事件訪問器
這篇文章主要介紹了在C#使用字典存儲事件示例及實(shí)現(xiàn)自定義事件訪問器的方法,是C#事件編程中的基礎(chǔ)知識,需要的朋友可以參考下2016-02-02

