c# - Selenium Chrome c# - 向远程发送 HTTP 请求时抛出错误
问题描述
我在 c# 中使用了很多 selenium 来自动化一些流程。但是硒抛出跟随错误
向远程 WebDriver 服务器发送 HTTP 请求以获取 URL http://localhost:56510/session/b9cbf278cef089229771901a70ce4d08/source 时抛出了一个带有空响应的异常。异常状态为 ConnectFailure,消息为:No es posible conectar con el servidor remoto
这个错误并不总是,是偶然的。我手动检查网站,它运行完美且速度非常快。
我创建网络浏览器的代码是
private IWebDriver CreateWebDriver()
{
var config = ConfigurationManager.AppSettings;
var downloadDirectory = ConfigurationManager.AppSettings["directory_download_default"];
// Configuración driver
ChromeOptions options = new ChromeOptions();
options.AddArgument("--disable-extensions");
options.AddUserProfilePreference("download.default_directory", downloadDirectory);
options.AddUserProfilePreference("download.prompt_for_download", "false");
options.AddArgument("--safebrowsing-disable-download-protection");
options.AddUserProfilePreference("safebrowsing", "enabled");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("--ignore-ssl-errors");
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["directory_chrome_binary"]))
options.BinaryLocation = ConfigurationManager.AppSettings["directory_chrome_binary"];
if (Convert.ToBoolean(config["chrome_headless"]))
options.AddArgument("--headless"); // Headless navegador sin interfaz grafica
var minutes = 5;
if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["minutes_web_timeout"]))
minutes = Convert.ToInt32(ConfigurationManager.AppSettings["minutes_web_timeout"]);
var chromeDriverPath = ConfigurationManager.AppSettings["directory_chrome_driver"];
var driver = new ChromeDriver(chromeDriverPath, options, new TimeSpan(0, minutes, 0));
driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, minutes, 0));
driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, minutes, 0));
driver.Manage().Timeouts().SetScriptTimeout(new TimeSpan(0, minutes, 0));
return driver;
}
我尝试将超时设置为 5 分钟(默认为 60 秒),但问题仍然存在。
在此先感谢您的帮助
解决方案
推荐阅读
- django - 由于缺少 url args 导致的 Django 错误渲染视图
- python - 如何在多处理中共享变量
- opengl - 场景光照计算顺序
- python - 在 heroku 上安装 requirements.txt 运行错误
- java - 从命令行编译 Netbeans 项目
- django - 如何使 Django 过滤器仅向某些用户显示?
- perl - Perl 在@INC 错误中找不到 Digest/SHA1.pm
- android - google play 应用安装与 Android 设备上的本地安装有区别吗?
- scala - 为什么我得到“找不到编译器镜像中的对象 scala.runtime”。升级依赖项时使用 scala-maven-plugin?
- java - 在 Groovy 中存根时出现 InvalidUseOfMatchersException