C# GetDevToolsSession() - is fixed forever to one tab/window
See original GitHub issueš Bug Report
When one calls GetDevToolsSession()
then the session is set to stone for the webdriver and cannot be changed anymore.
For instance when one opens a new tab/window after calling GetDevToolsSession()
once, it is impossible to get the DevToolsSession of the new tab/window.
When one closes the tab/window, then GetDevToolsSession()
will continue to return the ādeadā session. Trying to execute any command on that session will lead to an exception:
System.InvalidOperationException
HResult=0x80131509
Message=A command response was not received: Network.getAllCookies
Source=WebDriver
StackTrace:
at OpenQA.Selenium.DevTools.DevToolsSession.<SendCommand>d__31.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at OpenQA.Selenium.DevTools.DevToolsSession.<SendCommand>d__30`2.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at OpenQA.Selenium.DevTools.V93.Network.NetworkAdapter.<GetAllCookies>d__99.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at <Program>$.<<Main>$>d__0.MoveNext() in C:\src\devtoolssessionnewtab\DevToolsSessionNewTab\Program.cs:line 20
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at <Program>$.<Main>(String[] args)
To Reproduce
Program.cs
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools.V93.Network;
using var chromeDriver = new ChromeDriver();
chromeDriver.Url = "https://www.selenium.dev";
string originalWindow = chromeDriver.CurrentWindowHandle;
await DevToolsSessionNewTab.Helper.GetAllCookies(chromeDriver);
chromeDriver.SwitchTo().NewWindow(WindowType.Tab);
string newWindowHandle = chromeDriver.CurrentWindowHandle;
await DevToolsSessionNewTab.Helper.GetAllCookies(chromeDriver);
// this .Close() kills the dev tools session, no chance to ever retrieve a new one for the other tab
chromeDriver.SwitchTo().Window(originalWindow).Close();
chromeDriver.SwitchTo().Window(newWindowHandle);
chromeDriver.Url = "https://www.selenium.dev/documentation/webdriver/browser_manipulation/";
await DevToolsSessionNewTab.Helper.GetAllCookies(chromeDriver);
namespace DevToolsSessionNewTab
{
public static class Helper
{
public static Task GetAllCookies(ChromeDriver driver)
{
return driver.GetDevToolsSession()
.GetVersionSpecificDomains<OpenQA.Selenium.DevTools.V93.DevToolsSessionDomains>()
.Network
.GetAllCookies(new GetAllCookiesCommandSettings { });
}
}
}
Project file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-rc1" />
<PackageReference Include="Selenium.WebDriver.ChromeDriver" Version="93.0.4577.1500" />
</ItemGroup>
</Project>
Expected behavior
.GetDevToolsSession()
returns the DevToolsSession of the current active window
or
.GetDevToolsSession(string windowIdentifier)
returns the DevToolsSession of the window
or
Any way to create a DevToolsSession
manually. Right now itās impossible because the DevToolsSession
Start()
is internal
.
https://chromedevtools.github.io/devtools-protocol/ supports a GET
/json
or /json/list
where one can get all available devtools session.
Test script or set of commands reproducing this issue
See above code + github repo: https://github.com/schrufygroovy/devtoolssessionnewtab
Environment
OS: Windows 10 Browser: Chrome Browser version: 93.0.4577.63 Browser Driver version: 93.0.4577.1500 Language Bindings version: dotnet 4.0.0-rc1
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:10 (5 by maintainers)
Top GitHub Comments
Alright, I know this issue is closed, but those whoāve subscribed to it should receive this notification so as to get the updated information. Iāve changed things around for RC2, so previous comments about using
ResetDevToolsSession()
should be ignored. In RC2, the .NET bindings should detect that the initial target is being closed, and automatically reattach to a different target, which would make the code in the original issue report run cleanly.Iāll note that RC2 will still include a new method on
IDevTools
, but calledCloseDevToolsSession()
, the semantics of which are to callDispose()
on theDevToolsSession
object, and set the driverās internal reference tonull
, meaning that one can calldevToolsDriver.CloseDevToolsSession()
, then calldevToolsDriver.GetDevToolsSession()
to create a new session without issue.@schrufygroovy, @MalteFries
As of 2b67ece, Iāve added a
TerminateDevToolsSession
method on theIDevTools
interface, so users could do something like this (using a slightly-modified example from the original issue report, and assuming the definition ofDevToolsSessionNewTab.Helper.GetAllCookies()
remains as in that original post):Again, Iām not thrilled with this solution, but it does resolve the immediate problem. Expect that this API will evolve over time.