Click or drag to resize

WebBrowserExtensionsWaitForSelectorAsync Method

Version 112.3.0
Waits for a DOM element specified by the selector string to be added to or removed from the DOM. A simplified version of Puppeteer WaitForSelector. Uses a MutationObserver to wait for the element to become added or removed.

Namespace:  CefSharp
Assembly:  CefSharp (in CefSharp.dll) Version: 112.3.0.0 (112.3.0.0)
Syntax
public static Task<WaitForSelectorAsyncResponse> WaitForSelectorAsync(
	this IWebBrowser chromiumWebBrowser,
	string selector,
	TimeSpan? timeout = null,
	bool removed = false
)

Parameters

chromiumWebBrowser
Type: CefSharpIWebBrowser
ChromiumWebBrowser instance (cannot be null)
selector
Type: SystemString
querySelector for the element e.g. #idOfMyElement
timeout (Optional)
Type: SystemNullableTimeSpan
timeout
removed (Optional)
Type: SystemBoolean
(Optional) if true waits for element to be removed from the DOM. If the querySelector immediately resolves to null then the element is considered removed. If false (default) waits for the element to be added to the DOM.

Return Value

Type: TaskWaitForSelectorAsyncResponse
A Task that resolves when element specified by selector string is added to or removed from the DOM.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IWebBrowser. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Remarks
This function is typically used in conjunction with javascript that directly or indirectly adds/removes an element from the DOM. Unlike the puppeteer version navigations aren't handled internally, the method will throw a TimeoutException if a navigation occurs whilst waiting to resolve.
Examples
string script = "const newDiv = document.createElement('div'); newDiv.id = 'myElement'; document.body.append(newDiv);";
await Task.WhenAll(
    browser.WaitForSelectorAsync("#myElement");,
    chromiumWebBrowser.EvaluateScriptAsync(script));
See Also