簡體   English   中英

CefSharp 屏幕外 - 等待頁面渲染

[英]CefSharp offscreen - wait for page for render

我有如下問題。 我使用 CefSharp offscreen 進行網頁自動化,如下所示(我只打開一個頁面): 1. 打開頁面並等待它呈現*。 2. 使用 EvaluateScriptAsync,我將值放入輸入表單,然后使用相同的方法單擊網頁上的按鈕。 3.然后這個網頁上有一些JS檢查結果並顯示一條消息。 4. 當顯示消息時,我制作了一個屏幕截圖。 **

但是,我有兩個問題: * 我的 sulution 必須是互聯網速度證明。 當我使用 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 來檢查元素是否存在。 如果是,則加載頁面。

我注意到渲染時間可能會因您的硬件而異。 調用 EvaluateScriptAsync 后最多可能需要 5 秒才能呈現。 因此,如果您不想獲得過時的屏幕截圖,最好在調用 ScreenshotAsync() 之前進行更長的延遲。

Thread.Sleep(5000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM