How to get Selenium to work with ASP.NET Core?

Viewed 853

I made project in Visual Studio using the Blazor Server App template (I also tried this with MVC, but same problem), and downloaded both the Selenium.WebDriver and Selenium.WebDriver.GeckoDriver (since I'm using Firefox) from NuGets.

Whenever I run my application, and the webdriver instantiation is reached, I get this error:

System.InvalidOperationException: 'Failed to start browser C:\Program Files\Mozilla Firefox\firefox.exe: The handle is invalid. (os error 6) (SessionNotCreated)'`

Although I'm clueless, my gut feeling is telling me that it is trying to run the webdriver from within the scope of the web application running on IIS, and that somehow might be causing problems; i.e. a browser cannot start another browser. But I don't know if this is the case at all... If I make a .NET Core Console App template project and replicate the webdriver code, it works without a problem.

My code is really basic so there's almost no reason to post it, but here it is for completeness:

@page "/counter"
@using OpenQA.Selenium;
@using OpenQA.Selenium.Firefox;

<h1>Start Firefox GeckoDriver</h1>

<button class="btn btn-primary" @onclick="StartFirefox">Run</button>

@code {
    private void StartFirefox()
    {
        IWebDriver driver = new FirefoxDriver() {
            Url = "https://www.google.com/"
        };
    }
}

I put it in one of the Razor pages just to test it there. I've already tried putting it in Program.cs:Main as the first thing to run, even commenting out the host-builder. Regardless, IIS (I guess?) will always start up a browser first.

I have no idea what the error means. The handle is invalid isn't exactly helpful.

Relevant post: Selenium with GeckoDriver don't work on ASP.net Core

He says that he made it work by changing his profile from IIS Express to the name of his project. I tried the same, and it actually worked. But why?

enter image description here

0 Answers
Related