【问题标题】:Getting Exception when try to get html from UWP WebView (Exception from HRESULT: 0x80020101)尝试从 UWP WebView 获取 html 时出现异常(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,或者你可以修复我的代码吗?

【问题讨论】:

    标签: javascript uwp


    【解决方案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;" });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-12-14
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多