VB.Net Webview2 How can I get html source code?

ぐ巨炮叔叔 提交于 2020-12-06 14:57:07

问题


I sucessfully display a web site on WebView2 in my VB.net (Visual Studio 2017) project but can not get html souce code. Please advise me how to get html code.

My code:

Private Sub testbtn_Click(sender As Object, e As EventArgs) Handles testbtn.Click
        WebView2.CoreWebView2.Navigate("https://www.microsoft.com/")
End Sub

Private Sub WebView2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView2.NavigationCompleted
        Dim html As String = ?????
End Sub

Thank you indeed for your advise in advance.


回答1:


I've only just started messing with the WebView2 earlier today as well, and was just looking for this same thing. I did manage to scrape together this solution:

Dim html As String
html = Await WebView2.ExecuteScriptAsync("document.documentElement.outerHTML;")

' The Html comes back with unicode character codes, other escaped characters, and
' wrapped in double quotes, so I'm using this code to clean it up for what I'm doing.
html = Regex.Unescape(html)
html = html.Remove(0, 1)
html = html.Remove(html.Length - 1, 1)

Converted my code from C# to VB on the fly, so hopefully didn't miss any syntax errors.




回答2:


Adding to @Xaviorq8 answer, you can use Span to get rid of generating new strings with Remove:

html = Regex.Unescape(html)
html = html.AsSpan()[1..^1].ToString();


来源:https://stackoverflow.com/questions/62431464/vb-net-webview2-how-can-i-get-html-source-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!