U
U
Uncle Bogdan2021-06-11 11:34:43
Parsing
Uncle Bogdan, 2021-06-11 11:34:43

Do unknown errors randomly appear?

It just throws an unknown error. Decompilation fails. I added try, but it only got worse, now not only the application was hanging, but also Windows (hard reboot). What to do? Sometimes it also gives out "value cannot be null"

Code:

the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using HtmlAgilityPack; 

namespace MouArBiTp
{
    public partial class Form1 : Form
    {
        ChromiumWebBrowser browser;
        HtmlAgilityPack.HtmlDocument html = new HtmlAgilityPack.HtmlDocument();

        public Form1()
        {
            InitializeComponent();
            InitializeChromium();
            browser.FrameLoadEnd += Browser_FrameLoadEnd;
        }

        private async void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {
         //   await Task.Delay(1000);
            var source = await browser.EvaluateScriptAsync("document.documentElement.outerHTML");

            if(source != null)
            {
                try
                {
                    html.LoadHtml((string)source.Result);

                    var node = html.DocumentNode.SelectNodes("//*[@id='b-case-header']/ul[2]/li[4]/a");

                    if (node != null)
                    {
                        Debug.WriteLine(node[0].InnerText);
                        browser.ExecuteScriptAsync("document.evaluate(\"//*[@id='b-case-header']/ul[2]/li[4]/a\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();");
                    }
                    else
                    {
                        node = html.DocumentNode.SelectNodes("//*[@id='gr_case_partps']/table/tbody/tr/td[1]/div/ul/li/span/text()");
                        if (node != null)
                        {
                            BeginInvoke(new Action(() => plaintiffs.Text = "Истцы:\n" + node[0].InnerText.Trim()));
                            node = html.DocumentNode.SelectNodes("//*[@id='gr_case_partps']/table/tbody/tr/td[4]/div/ul/li/span/text()");

                            if (node != null)
                            {
                                BeginInvoke(new Action(() => Other.Text = "Иные лица:\n" + node[0].InnerText.Trim()));
                            }

                            node = html.DocumentNode.SelectNodes("//*[@id='gr_case_judges page-break']/table/tbody/tr/td/div/ul/li");

                            if (node != null)
                            {
                                BeginInvoke(new Action(() => judjeName.Text = "Имя судьи:\n" + node[0].InnerText.Trim()));
                            }
                        }
                    }
                }
                catch(Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }


            //    if (node != null)
            //    {
            //        browser.ExecuteScriptAsync("document.evaluate('//*[@id='b-case-header']/ul[2]/li[4]/a', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();");
            //    }
            }
        }

        void button1_Click(object sender, EventArgs e)
        {
            browser.Load($"https://kad.arbitr.ru/Card?number={textBox1.Text}");
        }

        void InitializeChromium()
        {

            CefSettings settings = new CefSettings();
            Cef.Initialize(settings);
            browser = new ChromiumWebBrowser("https://kad.arbitr.ru");
            Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
        }

        void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Cef.Shutdown();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-06-11
@motkot

I'll move my comments to an answer.

1. a specific line is written in the stack trace
2. well, you didn’t write try-catch at random, right?

In short, the error is in the html.LoadHtml(); line. By the way, for some reason source is null, but the condition that if source != null bypasses.

1. null is not source, but source.Result.
I wrote in one of the previous questions that you should not use EvaluateScript - just because it is difficult to control.
2.Why are you using BeginInvoke next? There is just Invoke and InvokeAsync

and you call GetSourceAsync not on the browser, but on the frame.
e.Frame.GetSourceAsync

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question