C#Selenium-如何使用代理进行身份验证?
我已经挣扎了两天,找不到解决方案。我首先使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题必须成为:为什么硒的方法:
等待NetworkInterceptor.startmonitoring();
在Windows表单上不起作用。之所以发生这种情况,是因为Windows表单是单线螺纹应用程序。因此,为了使其正常工作,您可以称为以下创建方法:
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: