Click or drag to resize

WebBrowserExtensionsWaitForSelectorAsync Method

Version 130.1.90
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: 129.0.110.0 (129.0.110.0)
Syntax
public static Task<WaitForSelectorAsyncResponse> WaitForSelectorAsync(
	this IWebBrowser chromiumWebBrowser,
	string selector,
	TimeSpan? timeout = null,
	bool removed = false
)

Parameters

chromiumWebBrowser  IWebBrowser
ChromiumWebBrowser instance (cannot be null)
selector  String
querySelector for the element e.g. #idOfMyElement
timeout  NullableTimeSpan  (Optional)
timeout
removed  Boolean  (Optional)
(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

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.
Example
C#
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