C#Selenium-如何使用代理进行身份验证?

发布于 2025-01-21 13:41:45 字数 1252 浏览 0 评论 0原文

我已经挣扎了两天,找不到解决方案。我首先使用Mozilla Firefox尝试了它。然后,我尝试使用Google Chrome,并再次遇到相同的错误。 如何将代理功能添加到浏览器中?更确切地说,如何通过添加用户属性来运行它?

当涉及“ networkInterceptor.startmonitoring()”代码时,它会卡住。浏览器打开,但页面不加载。

如果我在浏览器打开后手动输入链接,则需要用户名和密码。

注意:另外,还有不同的方法吗?

版本:Selenium Webdriver 4.0.0

代码:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

private async void Create()
{
    Proxy proxy = new Proxy();
    var proxyAddress = "host:port";
    proxy.HttpProxy = proxyAddress;
    proxy.SslProxy = proxyAddress;

    ChromeOptions options = new ChromeOptions();
    options.Proxy = proxy;

    IWebDriver driver = new ChromeDriver(options);

    NetworkAuthenticationHandler handler = new NetworkAuthenticationHandler()
    {
        UriMatcher = (d) => true,
        Credentials = new PasswordCredentials("user", "password")
    };

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

    driver.Navigate().GoToUrl("www.google.com");

    await networkInterceptor.StopMonitoring();
}

I've been struggling for two days and couldn't find a solution. I tried it first with mozilla firefox. Then I tried with google chrome and got the same error again.
How do I add the proxy feature to the browser? More precisely, how can I run it by adding user properties?

It gets stuck when it comes to "networkInterceptor.StartMonitoring()" code. The browser opens but the page does not load.

If I enter the link manually after the browser opens, it requires username and password.

Note : Also, is there a different method?

Version : Selenium Webdriver 4.0.0

Codes:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

private async void Create()
{
    Proxy proxy = new Proxy();
    var proxyAddress = "host:port";
    proxy.HttpProxy = proxyAddress;
    proxy.SslProxy = proxyAddress;

    ChromeOptions options = new ChromeOptions();
    options.Proxy = proxy;

    IWebDriver driver = new ChromeDriver(options);

    NetworkAuthenticationHandler handler = new NetworkAuthenticationHandler()
    {
        UriMatcher = (d) => true,
        Credentials = new PasswordCredentials("user", "password")
    };

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

    driver.Navigate().GoToUrl("www.google.com");

    await networkInterceptor.StopMonitoring();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2025-01-28 13:41:45

我认为问题必须成为:为什么硒的方法:等待NetworkInterceptor.startmonitoring();在Windows表单上不起作用。
之所以发生这种情况,是因为Windows表单是单线螺纹应用程序。因此,为了使其正常工作,您可以称为以下创建方法:

//First change your `Create()` method to return a Task instead of void.
private async void button1_Click(object sender, EventArgs e)
{
    await Task.Run(async () =>
    {
        await Create();
    });
}

I think that the question must become: why selenium's method: await networkInterceptor.StartMonitoring(); doesn't work on windows forms.
This occurs because windows forms is single threaded application. So in order to make it to work you might call the create method as below:

//First change your `Create()` method to return a Task instead of void.
private async void button1_Click(object sender, EventArgs e)
{
    await Task.Run(async () =>
    {
        await Create();
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文