首页 > 解决方案 > 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 秒),但问题仍然存在。

在此先感谢您的帮助

标签: c#selenium

解决方案


推荐阅读