0

Has anyone seen any documentation on the WebView2 DevToolsProtocolHelper? In another question I asked (How do I programmatically add a file to a fileupload control from a windows form to a webpage) it was suggested that I download and use the Microsoft.Web.WebView2.DevToolsProtocolExtensions. At first it seemed like it was going to be very straight forward to use but not so much.

Win forms App using c# and webview2

DevToolsProtocolHelper helper = webView21.CoreWebView2.GetDevToolsProtocolHelper()
Task<DOM.Node> t = helper.DOM.GetDocumentAsync();

 Task<int> querySelectorResponse = helper.DOM.QuerySelectorAsync(t.Result.NodeId, "#fileupload");
_ = helper.DOM.SetFileInputFilesAsync(new string[] { filename }, querySelectorResponse.Result);

These 4 lines of code should get the document and search for the node fileupload. I get nothing but errors and I have not seen any real examples or documentation on this.

Any help would be greatly appreciated.

**** UPDATE *****

        DevToolsProtocolHelper helper =  webView21.CoreWebView2.GetDevToolsProtocolHelper();
        DOM dom = helper.DOM;
        DOM.Node t = await dom.GetDocumentAsync(-1,true);
        int querySelectorResponse = await dom.QuerySelectorAsync(t.NodeId, "#fileupload");
        _ = helper.DOM.SetFileInputFilesAsync(new string[] { filename }, t.NodeId);

Here is the latest version of my code and it seems I have made progress. When I used CEFSHARP, the IDs I got back from Document and the #fileUpload were always the same and it worked in uploading the file.

With this code above, I am getting IDs but they are always different and I am not getting the file to upload.

Another update, when I run this code (from a button click on the winform) a second time, I do get the proper ID (504) for the int querySelectorResponse = await dom.QuerySelectorAsync(t.NodeId, "#fileupload") line of code. Again, still not getting the file to upload to the page.

Again, any help would be greatly appreciated

Chris Dunlop
  • 135
  • 1
  • 13

1 Answers1

0

The GetDevToolsProtocolHelper documentation is the 'How to' article on Using Chromium DevTools Protocol in WebView2.

Separately, you cannot use Task.Result with WebView2 tasks which I see you doing in the above code. WebView2 can only be used from the UI thread its created on and requires that UI thread to communicate task completions. You should be able to use await instead.

David Risney
  • 3,886
  • 15
  • 16
  • Thank you for this help. I have made progress but am still not getting the file to upload. See code changes in the edited question. – Chris Dunlop Jun 18 '21 at 21:35