尝试从 UWP WebView 获取 html 时出现异常(HRESULT 异常:0x80020101)

Posted

技术标签:

【中文标题】尝试从 UWP WebView 获取 html 时出现异常(HRESULT 异常:0x80020101)【英文标题】:Getting Exception when try to get html from UWP WebView (Exception from HRESULT: 0x80020101) 【发布时间】:2021-09-13 16:31:38 【问题描述】:

我需要从 WebView 获取 html。在网上找到如下代码:

public MainPage()

    this.InitializeComponent();
    WebView wb = new WebView();
    wb.Navigate(new Uri(@"https://www.microsoft.com/"));
    Thread.Sleep(1500);
    F(wb);

public async void F(WebView webView)

    var siteHtML = await webView.InvokeScriptAsync("eval", new string[]  "document.documentElement.outerHTML;" );

不幸的是,InvokeScriptAsync 方法返回错误,我根本不懂脚本。你有其他方法从 WebView 获取 html,或者你可以修复我的代码吗?

【问题讨论】:

【参考方案1】:

尝试从 UWP WebView 获取 html 时出现异常(HRESULT 异常:0x80020101)

问题是WebView没有导航完成,这种情况可以在NavigationCompleted事件中调用eval函数。在测试过程中,它运行良好。

wb.Navigate(new Uri(@"https://www.microsoft.com/"));
wb.NavigationCompleted += Wb_NavigationCompleted;

.....

private async void Wb_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)

    var siteHtML = await wb.InvokeScriptAsync("eval", new string[]  "document.documentElement.outerHTML;" );

【讨论】:

以上是关于尝试从 UWP WebView 获取 html 时出现异常(HRESULT 异常:0x80020101)的主要内容,如果未能解决你的问题,请参考以下文章