I've been new to automated test creation and have been dipping my hands on it recently. Every test seems to be running successfully individually except whenever I run them all together sequentially, sometimes, on random tests, I get an invalid session id error as described in the title. This happens completely randomly and on random tests too, it isn't always on the same ones where I get failures like that. I tried browsing to see if it was something related to my base automated test, perhaps it could be related to drivers overlapping but couldn't reach a solution that would make this stop happening. Here's the base I have for all my tests:

    public class BaseAutomatedTest : IDisposable
    public IWebDriver Driver;

    public BaseAutomatedTest()
        Driver = new TestDriverFactory().CreateDriver();

    public void Dispose()
    protected virtual void Dispose(bool disposing)
        //// Cleanup
        if (Driver != null)



All tests inherit the code above. Any ideas of what could possibly be going wrong? The internet made me inclined that my issue is with this base/driver initialization/closure but none of the solutions I've found seem to work. I'm using Selenium paired up with Chromedriver to execute my tests.

Edit: I've added a stack trace of an example of the error I'm getting. I'm using Chromedriver77 for this and I don't always get this error on this specific test, it's just for example purposes since it passes successfully sometimes:

    System.AggregateException : One or more errors occurred.
---- OpenQA.Selenium.WebDriverException : invalid session id
---- OpenQA.Selenium.WebDriverException : A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL http://localhost:51059/session/68f5fa5dfe6981eb48b5cdaf26f83abd/window. The status of the exception was ConnectFailure, and the message was: Unable to connect to the remote server
-------- System.Net.WebException : Unable to connect to the remote server
------------ System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it
----- Inner Stack Trace #1 (OpenQA.Selenium.WebDriverException) -----
RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
RemoteWebDriver.FindElement(String mechanism, String value)
RemoteWebDriver.FindElementById(String id)
<>c__DisplayClass16_0.<Id>b__0(ISearchContext context)
By.FindElement(ISearchContext context)
RemoteWebDriver.FindElement(By by)
ChangeUsernameTest.NavigateTo() line 91
ChangeUsernameTest.ChangeUsername_ConclusionNavigateToLinkValidation_Success(String username, String password) line 979
----- Inner Stack Trace #2 (OpenQA.Selenium.WebDriverException) -----
HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
HttpCommandExecutor.Execute(Command commandToExecute)
DriverServiceCommandExecutor.Execute(Command commandToExecute)
RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
BaseFunctionalTest.Dispose(Boolean disposing) line 34
BaseFunctionalTest.Dispose() line 22
ReflectionAbstractionExtensions.DisposeTestClass(ITest test, Object testClass, IMessageBus messageBus, ExecutionTimer timer, CancellationTokenSource cancellationTokenSource) line 79
----- Inner Stack Trace -----
HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
----- Inner Stack Trace -----
Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
  • Could you paste stacktrace and the exact error you are getting? Also please post version of chromedriver and selenium.
    – Prome
    Oct 30, 2019 at 9:21
  • @Prome added aditional information on EDIT section
    – E.T.
    Oct 30, 2019 at 10:35
  • @E.T. have you tried opening a ticket on Selenium github? Nov 4, 2019 at 7:09
  • Have you solved this random issue? I am struggling with a similar thing that happens on Windows Machines only, Mac is ok.
    – user54614
    Nov 5, 2022 at 5:31

1 Answer 1


I do not use C#, but as far as I know you should not be using WebDriver once Quit() was called.

Also, Selenium docs says Close() and Dispose() are already covered in Quit() so no need to call it again.

  • I've tried to adjust my solution based on your suggestion but nothing has changed in terms of test execution, I keep getting the completely random invalid session errors on random tests.
    – E.T.
    Oct 30, 2019 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.