Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 屏幕外-等待页面进行渲染_C#_Rendering_Cefsharp_Cefsharp.offscreen - Fatal编程技术网

C# 屏幕外-等待页面进行渲染

C# 屏幕外-等待页面进行渲染,c#,rendering,cefsharp,cefsharp.offscreen,C#,Rendering,Cefsharp,Cefsharp.offscreen,我有一个问题如下。我在屏幕外使用CefSharp实现网页自动化,如下所示(我只打开一个相同的页面): 1.打开页面并等待,直到它呈现*。 2.使用EvaluateScript Async,我将值输入表单,然后使用相同的方法单击网页上的按钮。 3.然后在这个网页上有一些JS检查结果并显示一条消息。 4.当信息显示时,我会做一个屏幕截图** 然而,我有两个问题: *我的解决方案必须是互联网的速度证明。当我使用BrowserLoadingStateChanged事件和IsLoading方法时,即使触发

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

然而,我有两个问题: *我的解决方案必须是互联网的速度证明。当我使用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);
                    });
                }


            }
        }

好的,在我的例子中,最好的解决方案是使用javascript检查是否存在id元素。如果是,则加载页面。

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

Thread.Sleep(5000);

加载,但可能仍然无法呈现。True-加载,但可能无法呈现,但仍然基于加载的信息和阅读其他一些论坛,我以使用thread sleep 200的解决方案结束,并在不同硬件页面上的所有测试中呈现。即使在CefSharp演示的屏幕外,也有人建议使用线程睡眠来等待页面呈现。如果某个进程将占用所有CPU,该怎么办?有没有办法检查Chrome的渲染应用程序/处理器/therad是否处于空闲状态?