我使用以下代码作为代理。然而,当chrome启动时,弹出窗口将弹出,程序将被锁定。
public async void StartDriver(string proxy)
{
var proxys = new Proxy();
ChromeDriverService chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions chromeOptions = new ChromeOptions();
bool flag = !string.IsNullOrEmpty(proxy);
if (flag)
{
proxys.Kind = ProxyKind.Manual;
proxys.IsAutoDetect = false;
proxys.SslProxy = proxy;
chromeOptions.Proxy = proxys;
}
driver = new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromMinutes(10));
await Task.Delay(2000);
}
我同样尝试了http或ssl。。。
StartDriver("88.55.66.77:8080");
或
StartDriver("http://username:pass@88.55.66.77:8080");
我无法使用某种代理启动浏览器。
我想要一个自动输入用户名和密码的代码。我不想要autoitx3.dll。
有办法启动一个安全的代理吗?
谢谢。
2021年,您可以使用Selenium 4.0和BiDi API通过授权代理进行请求。示例:
var options = new ChromeOptions {AcceptInsecureCertificates = true};
options.AddArgument("headless");
options.Proxy = new Proxy {HttpProxy = "1.1.1.1:12345", SslProxy = "1.1.1.1:12345", Kind = ProxyKind.Manual};
//options.AddArguments($"--proxy-server=http://1.1.1.1:12345");
var driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromMinutes(2));
NetworkAuthenticationHandler handler = new NetworkAuthenticationHandler()
{
UriMatcher = d => true, //d.Host.Contains("your-host.com")
Credentials = new PasswordCredentials("proxy_user", "proxy_pass")
};
var networkInterceptor = driver.Manage().Network;
networkInterceptor.AddAuthenticationHandler(handler);
networkInterceptor.StartMonitoring();
_driver.Navigate().GoToUrl("https://stackoverflow.com/");
networkInterceptor.StopMonitoring();
driver.Quit();
有办法启动安全代理吗?
有一个。您需要创建一个具有代理设置的chrome扩展。
manifest.json
{
"version": "0.1.0",
"manifest_version": 2,
"name": "%NAME IT AS YOU WANT%",
"permissions": [
"proxy",
"tabs",
"unlimitedStorage",
"storage",
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["background.js"]
},
"minimum_chrome_version":"22.0.0"
}
background.js
//note that it's a JS code. You can use any additional code to do anything :)
var config = {
mode: "fixed_servers",
rules: {
singleProxy: {
scheme: "http",
host: "%HOST%",
port: parseInt(%PORT%)
},
bypassList: ["foobar.com"]
}
};
chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
function callbackFn(details) {
return {
authCredentials: {
username: "%USERNAME%",
password: "%PASSWORD%"
}
};
}
chrome.webRequest.onAuthRequired.addListener(
callbackFn,
{urls: ["<all_urls>"]},
['blocking']
);
将其打包为存档。例如yourExt.dat
var proxy = "yourExt.dat";
var options = new ChromeOptions();
options.AddExtension(proxy);
var driver = new ChromeDriver(options);
Selenium 4有一个错误。执行基本授权时出现WebSocket异常https://github.com/SeleniumHQ/selenium/issues/10054