首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CefSharp offscreen -等待页面呈现

CefSharp offscreen -等待页面呈现
EN

Stack Overflow用户
提问于 2017-06-22 00:15:16
回答 2查看 2.2K关注 0票数 2

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

然而,我有两个问题:*我的解决方案必须是网速证明。当我使用BrowserLoadingStateChanged事件和IsLoading方法时,即使触发的事件没有完全加载网页-当我启动EavluateScriptAsync方法时,它会返回错误,因为页面没有完全加载。当然,我可以设置像ThreadSleep这样的东西,但它并不总是有效的-它强烈地依赖于你的网速。

**当我尝试制作截图时,它并不总是包含JS显示的结果消息-有时会有一个加载圈而不是消息。同样,我可以使用THreadSleep,但它并不总是有效。

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

代码语言:javascript
复制
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);
                    });
                }


            }
        }
EN

回答 2

Stack Overflow用户

发布于 2017-06-23 19:24:50

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

票数 0
EN

Stack Overflow用户

发布于 2021-01-20 18:36:49

我注意到渲染时间可能会因硬件的不同而显著不同。在调用EvaluateScriptAsync之后,渲染最多可能需要5秒。因此,如果不想截取过时的屏幕截图,最好在调用ScreenshotAsync()之前进行更长的延迟。

代码语言:javascript
复制
Thread.Sleep(5000);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44681335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档