-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to parse the result returned from ExecuteScriptAsync() in WebView2 #2365
Comments
WebView2 doesn't directly expose DOM objects as C# objects like the WebBrowser control did. I would recommend doing what you can in script to iterate over the input elements and extract the values you require and return that via ExecuteScriptAsync. The result string of ExecuteScriptAsync is the result of the execution of the JavaScript converted to JSON. So you'll need to parse the JSON string inputElementsIdAndValueAsJsonString = await webView.ExecuteScriptAsync(
"Array.from(document.getElementsByTagName('input')).map(inputElement => { " +
"return { " +
"'id': inputElement.id || inputElement.name, " +
"'value': inputElement.value " +
"} " +
"});");
JsonDocument inputElementsIdAndValueAsJsonDocument = JsonDocument.Parse(inputElementsIdAndValueAsJsonString);
foreach (var entry in inputElementsIdAndValueAsJsonDocument.RootElement.EnumerateArray())
{
Console.WriteLine(entry.GetProperty("id") + " " + entry.GetProperty("value"));
} It is also important to note that the conversion of script execution result to JSON is the same as JSON.stringify which only examines the owned properties of a script object. Most properties of DOM objects are inherited and so don't show up. That's why if in DevTools in the browser you do |
directly expose DOM objects as C# , verry good |
If you are interested in an alternative to using JavaScript see #77 (comment) |
Hi @david-risney , Thanks, |
It seems like there are many options for parsing JSON in C# and I'm not an expert on which are available in which versions of .NET. I used JsonDocument.Parse which is available for .NET Core 3.0, Core 3.1, 5, 6, 7 Preview 1 according to the docs. Another way to go might be JsonValue.Parse but I'm not sure if that's available for your version of .NET either JsonArray resultArray = (JsonArray)JsonValue.Parse(inputElementsIdAndValueAsJsonString);
foreach (JsonNode entry in resultArray)
{
JsonObject entryAsObject = entry.AsObject();
JsonNode idAsJsonNode;
JsonNode valueAsJsonNode;
entryAsObject.TryGetPropertyValue("id", out idAsJsonNode);
entryAsObject.TryGetPropertyValue("value", out valueAsJsonNode);
string id = idAsJsonNode.GetValue<string>();
string value = valueAsJsonNode.GetValue<string>();
Console.WriteLine("id " + id + " value " + value);
} There are other options for parsing JSON in C# that may work better for you, but that part is independent of WebView2. You can try searching for other JSON parsing options like this stackoverflow thread on JSON parsing in C#. |
Hi,
I am using ExecuteScriptAsync() method to get hold of the HTML in WebView2.
I have a requirement where I need to get all the input tags and iterate over them and store the value of all input tags in some hashmap for it to be used further in my code.
await webView2Browser.CoreWebView2.ExecuteScriptAsync("document.getElementsByTagName('input'));
I need it to convert it into html collection so that i can access html elements and their values.
Any help on how this could be done would be appreciated.
Thanks,
Priyanka
The text was updated successfully, but these errors were encountered: