HtmlDocument control in webView2

Harry Potter 21 Reputation points
2023-04-03T14:03:53.9266667+00:00

so i switched to webview2 cuz webbrowser was laggy

htmldocument isnt found in webview2 so

how do i make this code saves all text in html

HtmlDocument document = this.webBrowser1.Document;
string scriptName = "GetText";
object[] array = new string[0];
object[] array2 = array;
object[] args = array2;
object obj = document.InvokeScript(scriptName, args);
string text = obj.ToString();
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "Lua Script (.lua)|.lua|Text File (.txt)|.txt|All Files (.)|.";
saveFileDialog.Title = "Zeus Save file.";
saveFileDialog.ShowDialog();
try
{
string fileName = saveFileDialog.FileName;
string text2 = text;
string[] contents = new string[]
{
text2.ToString(),
""
};
File.WriteAllLines(saveFileDialog.FileName, contents);
}
catch (Exception)
{
}
}
}

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-04-03T15:26:51.59+00:00

    HtmlDocument is an IE class. WebView2 is based on Edge (webkit). You use native code interface to call script to access the dom

    https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/javascript

    you use the api to control the webview content

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2controller?source=recommendations&view=webview2-dotnet-1.0.1661.34

    Example to read all html

    string html = await webView2.ExecututeAsync("document.documentElement.outerHTML");
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.