M
M
Maxim K2021-10-15 12:53:40
C++ / C#
Maxim K, 2021-10-15 12:53:40

How to properly execute JS in CEFSharp?

For work I use CEFSharp. Much has been implemented and is working successfully. There is only one moment left, which I can’t win in any way, because. on the local machine it is not possible to check the site.
I wait for the page to fully load, then I execute the script. There can be 3-10 such pages - each in its own tab.
I found this method: private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e), implemented it for myself like this:

private void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
        {
            if (e.Frame.IsMain)
            {
                if (AddressBar.Text.Contains("ya"))  //проверка на адрес
                {
                    WebBrowser.ExecuteScriptAsync("document.querySelectorAll('#checkbox_panel > span').forEach(function(i){ i.click()})" + ";");
                }
            }
        }

At some point, or maybe immediately, the program stupidly closes, but it can work stably. How to "tie" this code with a check that the page is fully loaded, then execute the script? The site is heavy and can take a long time to render, and the script executes much earlier than the page is loaded and therefore throws it out of the program. Maybe I'm doing it all in the wrong way?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question