U
U
Uncle Bogdan2021-08-04 17:39:40
C++ / C#
Uncle Bogdan, 2021-08-04 17:39:40

Why is sometimes the text written 2 times, and not 1 time?

Such a joke. Sometimes it displays 2 texts at once, and not one.

610aa35e3ecd5901616727.png

I looked in the code through the debug, it seems it should not. What's the catch?

Website

string html = await browser.GetSourceAsync();

        angle = parser.ParseDocument(html);

        Plaintiffs.Text = string.Empty;

        foreach (IElement element in GetElementsBySelector(PlaintiffsSelector))
        {
             Plaintiffs.Text += element.TextContent.Trim() + "\n\n";
        }

        private List<IElement> GetElementsBySelector(string Selector)
        {
            IElement element = angle.QuerySelector(Selector);
            List<IElement> elements = new List<IElement>();

            int i = 1;

            while(element != null)
            {
                elements.Add(element);

                i++;

                Selector = Selector.Replace((i - 1).ToString(), i.ToString());      

                element = angle.QuerySelector(Selector);
                
            }

            return elements;
        }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2021-08-05
@motkot

To solve your problem, you need to write your frame load event handler as shown below and remove the Invoke call from everywhere . Asynchronous methods return tasks for that, so that, among other things, you can work in the UI thread (write the main code) and it does not freeze.
If there are several frames on the page, then this handler will be called when each of them is loaded

private readonly string url = "https://kad.arbitr.ru/Card?number=";

private void OnWebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
    Debug.WriteLine($"{nameof(OnWebBrowserFrameLoadEnded)}. " +
                    $"Frame.IsMain: {e.Frame.IsMain.ToString()}; e.Url: {e.Url};");

    if (e.Frame.IsMain && e.Url.Contains(url))
    {
        Invoke((MethodInvoker)(async () =>
        {
            await UpdateData();
        }));
    }
}

Use logging, otherwise you will get so tired of debugging bugs. Usually loggers are able to write the stream number in the logs as well.
ChromiumWebBrowser.FrameLoadEnd

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question