【问题标题】:Configure BrowserMobProxy in C#在 C# 中配置 BrowserMobProxy
【发布时间】:2020-03-15 19:57:07
【问题描述】:

谁能提供一个关于如何在 C# 中使用 Selenium Webdriver 配置 BrowserMobProxy 的正确示例?在我搜索的任何地方,它主要使用 Java 提供示例,而我们的框架是 C#,最重要的是我想使用 BrowserMobPRoxy。这样我就可以为我的 Web 应用程序捕获网络流量以测量性能

【问题讨论】:

  • 曾经得到这个答案吗?
  • 对此有何回应?

标签: c# selenium google-chrome webdriver browsermob-proxy


【解决方案1】:

也一直在尝试解决此问题,但 nuget 不适用于 .net 核心。但是 api 仍然是一个 API,因此它也可以从 c# 代码中访问。因此,您可以自己设置代理。我在 powershell 中做了代理启动,但它也可以在 c# 代码中完成。

在github下载BrowserMobProxy https://github.com/lightbody/browsermob-proxy

像这样启动bat文件,powershell e.g: .\browsermob -port 8080

做一些 api 调用。我用的是restsharp:

创建代理实例

var client = new RestClient("http://localhost:8080/proxy");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "text/plain");
var body = @" {""port"":8081}";
request.AddParameter("text/plain", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

创建 Har 文件,以便开始保存信息:

var client = new RestClient("http://localhost:8080/proxy/8081/har");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
IRestResponse response = client.Execute(request);

使用以下代码使用 selenium 设置代理:

ChromeOptions options = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.SslProxy = "http://localhost:8081";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);

开始测试

driver.Navigate().GoToUrl("https://www.selenium.dev/");

获取 har 文件

var client = new RestClient("http://localhost:8080/proxy/8081/har");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);

【讨论】:

    猜你喜欢
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-04-12
    • 1970-01-01
    相关资源
    最近更新 更多