此页面有问题。时间:2019-02-08 标签:c#webview2

This page is having a problem. c# Webview2

我在 VS 2019 中构建了一个 C# windows 表单应用程序,其中包括用于创建浏览器的 Webview2(最新预发布版本)。应用程序加载页面并检查特定内容。如果内容不存在,它会重新加载页面。如果内容存在(尚未发生),它会填写一些字段并单击按钮。

该应用程序在一段时间内运行良好。现在每隔一段时间我就会在 Webview2 控件中得到以下内容:

This page is having a problem. Please come back to it later

You could also: Open a new tab Refresh the page

Error Code: STATUS_ACCESS_VIOLATION

我 运行 应用程序处于调试模式以尝试捕获错误。捕捉无一例外。如果我在 Webview2 控件的 ContentLoading 事件中放置一个断点,它永远不会在我看到此错误页面之前触发。我不确定是什么触发了它或如何阻止它。

在 Form_load 我调用以下内容:

async void checksites()
{
    await wv2Dig.EnsureCoreWebView2Async();
    wv2Dig.CoreWebView2.Navigate(strURL);
}

并且 ContentLoading 将调用下面的函数,该函数尝试在页面加载时检查内容,以便在页面完全加载之前做出决定。现在它只是触发了我们不想要的按钮。这是代码:

async Task DigPageCheck()
        {
            iDigCount += 1; //count of number of checks
            txtDig.Text = iDigCount.ToString(); //display count

            string strResult = await wv2Dig.CoreWebView2.ExecuteScriptAsync("document.documentElement.innerHTML;");  
            //get innerHTML
            strResult = Regex.Unescape(strResult);
            strResult = strResult.Remove(0, 1);
            strResult = strResult.Remove(strResult.Length - 1, 1);
            //check for button we want
            int loc = strResult.IndexOf("btn btn-primary btn-lg btn-block btn-leading-ficon add-to-cart-button"); 
            //check for button we want
            int loc2 = strResult.IndexOf("btn btn-disabled btn-lg btn-block add-to-cart-button"); x
            while (loc == -1 && loc2 == -1)
            {
                //while neither button exists wait and check again
                await Task.Delay(200);
                strResult = await wv2Dig.CoreWebView2.ExecuteScriptAsync("document.documentElement.innerHTML;");
                strResult = Regex.Unescape(strResult);
                strResult = strResult.Remove(0, 1);
                strResult = strResult.Remove(strResult.Length - 1, 1);
                loc = strResult.IndexOf("btn btn-primary btn-lg btn-block btn-leading-ficon add-to-cart-button");
                loc2 = strResult.IndexOf("btn btn-disabled btn-lg btn-block add-to-cart-button");

            }
            //the button we want has been found 
            if (loc != -1)
            {
                iDig += 1;
                string mSubj = "Found";
                try
                {
                    sendSMS(mSubj, strDigURL);
                }
                catch
                {
                    label3.Text = "Email Error";
                }
                var functionString = string.Format(@"document.getElementsByClassName('btn btn-primary btn-lg btn-block btn-leading-ficon add-to-cart-button')[0].click();");
                await wv2Dig.CoreWebView2.ExecuteScriptAsync(functionString);
                
            }
            else //button we don't want found
            {
                if (bRun) //app is in run state, it can be paused
                    wv2Dig.CoreWebView2.Reload(); //Reload the page and check again
            }
        }

似乎在重新加载时的某个时候出现了错误页面。

通过删除 WebView2 预发行版 0.9.682 并安装预发行版 0.9.579 解决了以下问题

And I have a new issue that popped up randomly. The app freezes on await wv2Dig.EnsureCoreWebView2Async(); which is in a function called from Form_Load

我在执行 wv2Dig.CoreWebView2.Reload() 之前添加了 wv2Dig.CoreWebView2.Stop(),此后我没有发现问题。我认为这可能已经解决了问题。我说我认为是因为程序 运行 一段时间没有问题,然后开始出现问题。

在此过程中,我还降级了 WebView2 的 pre-release 版本,因为 WebView2 以某种方式完全停止工作。