Hi - I was finding that sometimes the CDP client was unexpectedly closed.
It seems that it was because I was logging a DOM element in the browser console, and this was causing an exception when the console log message gets reported through CDP.
Specifically, in CdpPage.cs (around lines 1100 to 1250), PuppeteerSharp.Cdp.CdpPage.Client_MessageReceived handles exceptions by closing PrimaryTargetClient.
This includes the case where e.MessageID == "Runtime.consoleAPICalled", and OnConsoleAPIAsync throws an exception.
OnConsoleAPIAsync converts all message arguments to either a CdpJSHandle or a CdpElementHandle, using ctx.CreateJSHandle. These are passed in an IJSHandle[] to AddConsoleMessageAsync.
AddConsoleMessageAsync then performs an explicit cast to CdpJSHandle, which fails for any CdpElementHandle in the converted arguments.
Possible solutions:
-
Discard any CdpElementHandles at the top of AddConsoleMessageAsync
values = values.Where(v => v is CdpJSHandle).ToArray();
This would mean the log on the C# side doesn't exactly match the log on the browser side.
-
Add an explicit cast from CdpElementHandle to CdpJSHandle
There may be reasons to avoid this.
-
Modify AddConsoleMessageAsync to handle both of CdpElementHandle and CdpJSHandle from the passed array.
This method seems to just use the RemoteObject field, which exists in both, so potentially not too complicated to implement.
Hi - I was finding that sometimes the CDP client was unexpectedly closed.
It seems that it was because I was logging a DOM element in the browser console, and this was causing an exception when the console log message gets reported through CDP.
Specifically, in CdpPage.cs (around lines 1100 to 1250), PuppeteerSharp.Cdp.CdpPage.Client_MessageReceived handles exceptions by closing PrimaryTargetClient.
This includes the case where e.MessageID == "Runtime.consoleAPICalled", and OnConsoleAPIAsync throws an exception.
OnConsoleAPIAsync converts all message arguments to either a CdpJSHandle or a CdpElementHandle, using ctx.CreateJSHandle. These are passed in an IJSHandle[] to AddConsoleMessageAsync.
AddConsoleMessageAsync then performs an explicit cast to CdpJSHandle, which fails for any CdpElementHandle in the converted arguments.
Possible solutions:
Discard any CdpElementHandles at the top of AddConsoleMessageAsync
values = values.Where(v => v is CdpJSHandle).ToArray();This would mean the log on the C# side doesn't exactly match the log on the browser side.
Add an explicit cast from CdpElementHandle to CdpJSHandle
There may be reasons to avoid this.
Modify AddConsoleMessageAsync to handle both of CdpElementHandle and CdpJSHandle from the passed array.
This method seems to just use the RemoteObject field, which exists in both, so potentially not too complicated to implement.