selenium: [🐛 Bug]: No connection could be made because the target machine actively refused it.
What happened?
Hi dear developers.
So what I currently struggle with is a problem with C# Selenium with SocketException for ChromeDriver:
[09:30:07 ] OpenQA.Selenium.WebDriverException : An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://localhost:23391/session/9e1b9a2ff8740b399050adc85714d15d/element. The exception message was: No connection could be made because the target machine actively refused it. (localhost:23391)
09:30:07 ----> System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it. (localhost:23391)
[09:30:07 ] ----> System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it.
I’ve done some digging into this to actually find what’s wrong. Setup of the test is pretty straightforward, driver is being initialized with ChromeOptions: “–start-maximized”, “–no-sandbox”, “–proxy-server=‘direct://’”, “–proxy-bypass-list=*”, This error occurs not only on CI/CD but my local machine also, what’s worth mentioning is that for 60+ automated test cases, the error comes usually once per run.
Configuration setup looks like this:
- ChromeDriver is being run from the package Selenium.WebDriver.ChromeDriver 113.0.5672.6300
- Test are runnig by local chromedriver -> local test, no grid, docker, or cloud is being used.
- CI/CD are running by the TeamCity agent, tests are being invoked by the usage of CommandLine with the command “dotnet test”
- All packages are up to date
- Chrome is up to date 113.0.5672.127 (64-bit)
- .NET SDK 5.0.408
- No proxy is set on the machine, on the server or the ChromeOptions
- No VPN is set on any of the machines
I’ve tried all the solutions I could find by searching all across the internet by found no good. What’s even funnier, the downgrade from .NET 6.0 to .NET 5.0 project reduced this error by a lot (From 3-4 crashes to 1 or none)
How can we reproduce the issue?
I cannot share the source code of my repo due to the fact that it's unallowed by my contract but I'll try to do my best to describe it.
Crash of the webdriver happens mostly at random. There's no part of the code which triggers it at the same place currently. For example, if test consists of 20 steps to do, it can fail at 1 step, 15, or even the last one.
But what I think a hint may be is one NuGet Package & few methods used.
- I'm using DotNetSeleniumExtras.WaitHelpers package in ver 3.11.0 which catches most of the time StaleElementReference exceptions, one test was waiting for an angular overlay to drop with method ExpectedConditions.InvisibilityOfElementLocated which was crashing quite often, deleting it and using Thread.Sleep(XAmmountOfTime) solved the problem.
Same thing goes for the method ExpectedConditions.UrlContains("some string of an url")
- The method that also crashes from time to time is driver.Navigate().GoToUrl("some url")
The more tests are there, the higher chance of the error to occur
Relevant log output
09:30:07 Error Message:
09:30:07 OpenQA.Selenium.WebDriverException : An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://localhost:23391/session/9e1b9a2ff8740b399050adc85714d15d/element. The exception message was: No connection could be made because the target machine actively refused it. (localhost:23391)
09:30:07 ----> System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it. (localhost:23391)
09:30:07 ----> System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it.
09:30:07 TearDown : OpenQA.Selenium.WebDriverException : An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://localhost:23391/session/9e1b9a2ff8740b399050adc85714d15d/screenshot. The exception message was: No connection could be made because the target machine actively refused it. (localhost:23391)
09:30:07 ----> System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it. (localhost:23391)
09:30:07 ----> System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it.
09:30:07 Stack Trace:
09:30:07 at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
09:30:07 at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
09:30:07 at OpenQA.Selenium.WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
09:30:07 at OpenQA.Selenium.WebDriver.FindElement(String mechanism, String value)
09:30:07 at OpenQA.Selenium.By.<.ctor>b__11_0(ISearchContext context)
09:30:07 at OpenQA.Selenium.By.FindElement(ISearchContext context)
09:30:07 at OpenQA.Selenium.WebDriver.FindElement(By by)
09:30:07 at SeleniumExtras.WaitHelpers.ExpectedConditions.<>c__DisplayClass17_0.<InvisibilityOfElementLocated>b__0(IWebDriver driver)
09:30:07 at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition, CancellationToken token)
09:30:07 at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
09:30:07 at Eagle.Framework.Models.E2E.Wait.Until(Func`2 condition) in C:\BuildAgent\work\d20cd7016e022376\Eagle.Framework\Models\E2E\Wait.cs:line 30
09:30:07 at Eagle.Framework.PageObjects.mCalendar.mCalendar.SaveConference() in C:\BuildAgent\work\d20cd7016e022376\Eagle.Framework\PageObjects\mCalendar\mCalendar.cs:line 303
09:30:07 at Eagle.E2E.Test.StepDefinitions.mCalendarStepDefinitions.ThenKonferencjaZostajeZapisana() in C:\BuildAgent\work\d20cd7016e022376\Eagle.E2E.Test\StepDefinitions\mCalendarStepDefinitions.cs:line 302
09:30:07 at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
09:30:07 at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStepMatch(BindingMatch match, Object[] arguments, TimeSpan& duration)
09:30:07 at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.ExecuteStep(IContextManager contextManager, StepInstance stepInstance)
09:30:07 at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnAfterLastStep()
09:30:07 at TechTalk.SpecFlow.TestRunner.CollectScenarioErrors()
09:30:07 at Eagle.E2E.Test.Features.MKalendarz_KonferencjeFeature.ScenarioCleanup()
09:30:07 at Eagle.E2E.Test.Features.MKalendarz_KonferencjeFeature.Sale_Edycja() in C:\BuildAgent\work\d20cd7016e022376\Eagle.E2E.Test\Features\mCalendarConferences.feature:line 178
09:30:07 --HttpRequestException
Operating System
Windows 10, Windows Server 2016 Standard
Selenium version
C# Selenium.WebDriver 4.9.1, NUnit 3.13.3, NUnit3TestAdapter 4.4.2
What are the browser(s) and version(s) where you see this issue?
GoogleChrome 113.0.5672.127 (64-bit)
What are the browser driver(s) and version(s) where you see this issue?
Selenium.WebDriver.ChromeDriver 113.0.5672.6300
Are you using Selenium Grid?
No
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 19 (8 by maintainers)
Well, I’ll try to respond due to some similarities to my own problem. So basically, while I was running selenium on a specific WindowsServer that someone previously owned( and quit the job without leaving any documentation or any insight), my tests were shutting down randomly. Why did it happen? As it turns out, someone created a Task in windows TaskScheduler, to run at 7-8-9AM (3 in total) via Powershell to start tests with ChromeDriver, then after all the tests we’re colliding with one another, as the end of the job, there was a task to shutdown all driver.exe processes.
I discovered it after a while after posting this thread. Maybe check for any jobs/services/powershells/task etc on the machine that Your tests are running on. It could be even the same session that was not shutdown after debugging set on a specific port that takes this port until the task is completed.
Based on my personal experience, for most of the runs, the issue occurred on a slight timeout, around 3-4 seconds. It looks like waiting and then throwing an error. I found one case in which the test got stuck for about an hour (teamcity agent closed the run due to the timeout settings) in this method. From my approach, we can close this thread, because well, it’s even hard to pull off without giving a set of as many automated tests possible with the usage of that particular library. Maybe there should be a prevention mechanism for this, I really don’t know.
Anyway, I really appreciate Your time Titus, thanks for looking at it. Maybe selenium documentation page should have like one big red banner with “Don’t use nuget packages with waits” 😆