【问题标题】:CefSharp offscreen - wait for page for renderCefSharp offscreen - 等待页面渲染
【发布时间】:2023-03-26 17:32:01
【问题描述】:

我有一个问题如下。我使用 CefSharp offscreen 进行网页自动化如下(我只打开一个相同的页面): 1. 打开页面并等待它呈现*。 2. 使用 EvaluateScriptAsync 我将值输入表单,然后使用相同的方法单击网页上的按钮。 3.然后这个网页上有一些JS检查结果并显示消息。 4.当消息显示时,我会截屏。 **

但是,我有两个问题: * 我的解决方案必须是 Internet 速度证明。当我使用 BrowserLoadingStateChanged 事件和 IsLoading 方法时,即使触发的事件没有完全加载网页 - 当我启动 EavluateScriptAsync 方法时,它会返回错误,因为页面没有完全加载。当然,我可以说 ThreadSleep 之类的东西,但它并不总是有效 - 它在很大程度上取决于您的互联网速度。

** 当我尝试截屏时,它并不总是包含 JS 显示的结果消息 - 有时会出现加载圆圈而不是消息。在这里我可以再次使用 THreadSleep,但它并不总是有效。

你有什么想法吗?提前致谢。

private static void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
        {
            // Check to see if loading is complete - this event is called twice, one when loading starts
            // second time when it's finished
            // (rather than an iframe within the main frame).
            if (!e.IsLoading)
            {
                // Remove the load event handler, because we only want one snapshot of the initial page.
                browser.LoadingStateChanged -= BrowserLoadingStateChanged;

                Thread.Sleep(1800); // e. g. but it isn't a solution in fact

                var scriptTask = browser.EvaluateScriptAsync("document.getElementById('b-7').value = 'something'");



                scriptTask = browser.EvaluateScriptAsync("document.getElementById('b-8').click()");

                //scriptTask.Wait();

                if (browser.IsLoading == false)
                {
                    scriptTask.ContinueWith(t =>
                    {

                        //Give the browser a little time to render
                        //Thread.Sleep(500);
                        Thread.Sleep(500); // still not a solution

                        // Wait for the screenshot to be taken.
                        var task = browser.ScreenshotAsync();
                        task.ContinueWith(x =>
                        {
                            // Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
                            var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");

                            Console.WriteLine();
                            Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);

                            // Save the Bitmap to the path.
                            // The image type is auto-detected via the ".png" extension.
                            task.Result.Save(screenshotPath);

                            // We no longer need the Bitmap.
                            // Dispose it to avoid keeping the memory alive.  Especially important in 32-bit applications.
                            task.Result.Dispose();

                            Console.WriteLine("Screenshot saved.  Launching your default image viewer...");

                            // Tell Windows to launch the saved image.
                            Process.Start(screenshotPath);

                            Console.WriteLine("Image viewer launched.  Press any key to exit.");
                        }, TaskScheduler.Default);
                    });
                }


            }
        }

【问题讨论】:

    标签: c# rendering cefsharp cefsharp.offscreen


    【解决方案1】:

    好的,所以在我的情况下,最好的解决方案是使用 javascript 来检查 id 元素是否存在。如果是,则页面已加载。

    【讨论】:

    • 已加载,但可能仍未呈现。
    • True - 已加载但可能无法渲染,但仍基于已加载的信息并阅读其他一些论坛呈现。即使在屏幕外的 CefSharp 演示中,也有人建议使用线程睡眠来等待页面渲染。
    • 如果某个进程会用完所有的 CPU 怎么办?可能有办法检查 Chrome 的渲染应用/处理器/therad 是否已空闲?
    【解决方案2】:

    我注意到渲染时间可能会因您的硬件而有很大差异。调用 EvaluateScriptAsync 后最多可能需要 5 秒才能呈现。因此,如果您不想获得过时的屏幕截图,最好在调用 ScreenshotAsync() 之前做更长的延迟。

    Thread.Sleep(5000);
    

    【讨论】:

      猜你喜欢
      • 2020-04-02
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      相关资源
      最近更新 更多