question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jimevanscommented, Sep 28, 2021

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 called CloseDevToolsSession(), the semantics of which are to call Dispose() on the DevToolsSession object, and set the driverā€™s internal reference to null, meaning that one can call devToolsDriver.CloseDevToolsSession(), then call devToolsDriver.GetDevToolsSession() to create a new session without issue.

1reaction
jimevanscommented, Sep 21, 2021

@schrufygroovy, @MalteFries

As of 2b67ece, Iā€™ve added a TerminateDevToolsSession method on the IDevTools interface, so users could do something like this (using a slightly-modified example from the original issue report, and assuming the definition of DevToolsSessionNewTab.Helper.GetAllCookies() remains as in that original post):

using (var driver = new ChromeDriver())
{
    var devToolsDriver = driver as IDevTools;
    driver.Url = "https://www.selenium.dev";
    string originalWindow = driver.CurrentWindowHandle;
    await DevToolsSessionNewTab.Helper.GetAllCookies(driver);

    string currentTargetId = string.Empty;

    driver.SwitchTo().NewWindow(WindowType.Tab);
    string newWindowHandle = driver.CurrentWindowHandle;
    await DevToolsSessionNewTab.Helper.GetAllCookies(driver);

    // this .Close() kills the dev tools session, no chance to ever retrieve a new one for the other tab
    driver.SwitchTo().Window(originalWindow).Close();
    devToolsDriver.TerminateDevToolsSession();

    driver.SwitchTo().Window(newWindowHandle);
    driver.Url = "https://www.selenium.dev/documentation/webdriver/browser_manipulation/";
    await DevToolsSessionNewTab.Helper.GetAllCookies(driver);
}

Again, Iā€™m not thrilled with this solution, but it does resolve the immediate problem. Expect that this API will evolve over time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Selenium 4 C# Chrome DevTools - Stack Overflow
Try this IDevTools devTools = driver.OriginDriver as IDevTools; DevToolsSession session = devTools.GetDevToolsSession(); FetchAdapterĀ ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found