System.InvalidOperationException (SessionNotCreated) and Browser Window opens non-automated on some machines
When I drive Edge with Selenium (.NET) on a dev machine (Windows 11), everything works fine.
When I do the same on an office PC (also Windows 11), I get exception
System.InvalidOperationException: session not created: probably user data directory is already in use, please specify a unique value for --user-data-dir argument, or don't use --user-data-dir (SessionNotCreated)
but the browser windows still opens - apparently in non-automated mode.
I've tried all obvious variants: Specify a different user data dir, specify different profiles, make sure that there is no edge instance running in Taskmanager, try with the "Edge Dev" version, etc.
When I look at the driver logs and run the logged command line in a terminal, I do not see any errors, but also get different behavior.
Dev Machine (working as expected):
Office Machine (does not open in "automation mode"):
What could cause this different behavior? Are there any command-line options that I could apply to make the webdriver work in all environments?
Hey Andreas, I am seeing the same issue in the same environment. It only started happening to us yesterday, when we restarted our .NET server application that runs the selenium operations.
I tried specifying a unique user-data-dir for each new driver / browser instance, but then I get an error about the devtools port not being properly closed last time.
Curious if the EdgeDriver team have any feedback.
No response yet. Note that the issue is intermittent for me. Failed repeatedly, then worked for a few days, then failed continuously again.
Could not identify any pattern, yet.
Adding this to the options solved it for me for some reason, don't ask me why. But it works as a temporarily solution.
"--remote-debugging-port=9222"
Thank you Gettokiwi, your fix worked for me with a slight adjustment. In my case, I am running multiple browsers concurrently so Selenium threw an exception when I tried to start a 2nd browser with the same debug port. I used the following bit of code from here to find a free port for each browser when I initialize them.
@Gettokiwi : I can also confirm that this workaround solves the issue. Apparently the default "=0" setting for the port does not work in some environments. Could not clarify why exactly port selection would be failing, though.
@guangyuexu: As you are apparently investigating a similar exception in #189 - is there any chance that these are related?
Even if not, it would be great if the exception message were corrected as the reference to the user directory is quite misleading ...
@Gettokiwi I am trying to run the test cases in the Azure Release pipeline, but I am still getting the same error even after adding the argument. Can you please suggest a solution with a code snippet that you are currently using?
@patilss893
This is how we use it at the moment, keep in mind that we are still on selenium 3.x, so for 4.x you have to provide the args in a different way.
EdgeOptions options = new EdgeOptions();
options.BrowserVersion = "latest";
if (headlessMode)
{
options.AddAdditionalCapability("headless", true);
}
var edgeOptions = new Dictionary<string, object>
{
["args"] = new string[]
{
"--start-maximized",
"--disable-dev-shm-usage",
"--disable-back-forward-cache" ,
"--remote-debugging-port=9222"
}
};
options.AddAdditionalCapability("ms:edgeOptions", edgeOptions);
var sd = EdgeDriverService.CreateDefaultService(Configuration.Data.EdgeForTestingPath, "msedgedriver.exe");
return new EdgeDriver(sd, options, TimeSpan.FromMinutes(3));