升级CEFSharp 57至63后ChromeDriver get命令未等待页面加载完成
get Command in Selenium with CEFSharp 63 Hey Shay, I’ve dealt with exactly this kind of issue when upgrading CEFSharp versions—those subtle changes to page load event handling can throw off Selenium’s default behavior. Let’s walk through the likely causes and practical fixes:
1. Replace Implicit Wait with Explicit Wait for Critical Elements
CEFSharp 63 adjusted how it signals "page load complete" to Selenium, so the default get method no longer waits reliably for the full DOM to be ready. Instead of relying on that, use Selenium’s explicit wait to target a key element that you know will be present when the page is fully loaded:
// Initialize your CEFSharp driver var driver = new CefSharpDriver(); // Navigate to the URL driver.Navigate().GoToUrl("your-target-url"); // Wait up to 10 seconds for a critical element to exist var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(ExpectedConditions.ElementExists(By.Id("critical-element-id"))); // Now you can safely interact with the DOM var element = driver.FindElement(By.Id("critical-element-id"));
This approach bypasses the unreliable default load signal and waits for something that actually matters for your use case.
2. Hook into CEFSharp’s Native Load Events
CEFSharp exposes native events that fire when frames or the entire document finish loading. You can bind to these events to ensure you only proceed after the page is truly ready:
var cefBrowser = driver.GetCefBrowser(); // Get the underlying CEF browser instance // Wait for the main frame's DocumentComplete event var tcs = new TaskCompletionSource<bool>(); EventHandler<FrameLoadEndEventArgs> frameLoadHandler = (sender, args) => { if (args.Frame.IsMain) { tcs.TrySetResult(true); cefBrowser.FrameLoadEnd -= frameLoadHandler; // Unsubscribe to avoid leaks } }; cefBrowser.FrameLoadEnd += frameLoadHandler; // Navigate driver.Navigate().GoToUrl("your-target-url"); // Wait for the event to trigger await tcs.Task; // Now interact with the DOM
This directly leverages CEFSharp’s internal load logic, which is more reliable than Selenium’s default binding in newer versions.
3. Verify Selenium-CEFSharp Driver Compatibility
Make sure you’re using a version of the Selenium CEFSharp driver that’s explicitly built for CEFSharp 63. Older drivers might not handle the updated load event signaling correctly. Check the driver’s release notes to confirm compatibility with your CEFSharp version.
4. Poll for document.readyState
If the above methods don’t work, you can explicitly check the browser’s document.readyState via JavaScript until it returns complete:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").ToString() == "complete");
This is a brute-force but effective way to ensure the DOM is fully loaded, even if CEFSharp’s default signals are off.
内容的提问来源于stack exchange,提问作者Shay

