You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For those sponsoring the project I'm happy to provide guidance in porting from the IE WebBrowser.
IMPORTANT
In the interest of getting some real world code examples the first three people to provide code examples I'll provide guidance without change.
If you are a sponsor and require assistance please post your existing code here. For those not currently sponsoring I can provide code examples starting from $50USD via a One Time GitHub sponsorship. Please post your code first and then ask how much to provide a detailed example.
Some general notes and comments are below:
Threading
The API is designed to be Async and should be used with async all the way. Current limitation of WebView2 is that DevTools protocol method calls can only happen on the UI Thread (Issue #2) so writing sync code will deadlock very very easily and should be avoided at all costs.
// Get element by Id// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector// https://www.javascripttutorial.net/javascript-dom/javascript-queryselector/HtmlElementelement=awaitdevtoolsContext.QuerySelectorAsync("#myElementId");//Strongly typed element types//Only a subset of element types have been added so far, use HtmlElement as a generic type for all othersvarhtmlDivElement=awaitdevToolsContext.QuerySelectorAsync<HtmlDivElement>("#myDivElementId");varhtmlSpanElement=awaitdevToolsContext.QuerySelectorAsync<HtmlSpanElement>("#mySpanElementId");varhtmlSelectElement=awaitdevToolsContext.QuerySelectorAsync<HtmlSelectElement>("#mySelectElementId");varhtmlInputElement=awaitdevToolsContext.QuerySelectorAsync<HtmlInputElement>("#myInputElementId");varhtmlFormElement=awaitdevToolsContext.QuerySelectorAsync<HtmlFormElement>("#myFormElementId");varhtmlAnchorElement=awaitdevToolsContext.QuerySelectorAsync<HtmlAnchorElement>("#myAnchorElementId");varhtmlImageElement=awaitdevToolsContext.QuerySelectorAsync<HtmlImageElement>("#myImageElementId");varhtmlTextAreaElement=awaitdevToolsContext.QuerySelectorAsync<HtmlImageElement>("#myTextAreaElementId");varhtmlButtonElement=awaitdevToolsContext.QuerySelectorAsync<HtmlButtonElement>("#myButtonElementId");varhtmlParagraphElement=awaitdevToolsContext.QuerySelectorAsync<HtmlParagraphElement>("#myParagraphElementId");
GetElementsByTagName
// Get elements by tag name// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAllvardivElements=awaitdevtoolsContext.QuerySelectorAllAsync("div");varspanElements=awaitdevtoolsContext.QuerySelectorAllAsync("span");
Element Click
Use the HtmlElement.ClickAsync() method to click a button. This method uses mouse emulation, moves the mouse over the element and sends down/up events.
// Get element by Id// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector// https://www.javascripttutorial.net/javascript-dom/javascript-queryselector/varelement=awaitdevtoolsContext.QuerySelectorAsync("#myElementId");// Get elements by tag name// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAllvarelements=awaitdevtoolsContext.QuerySelectorAllAsync("#div");//Click The elementawaitelement.ClickAsync();
Use the HtmlElement.InvokeMember() method to click a button. This method calls the HTMLElement.click method.
// Get element by Id// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorvarelement=awaitdevtoolsContext.QuerySelectorAsync("#myElementId");//Click The elementawaitelement.InvokeMemberAsync("click");
InnerHtml/InnerText
HtmlElement.SetPropertyValueAsync/HtmlElement.GetPropertyValueAsync can be used to get properties of a HtmlElement
awaitusingvardevtoolsContext=awaitcoreWebView2.CreateDevToolsContextAsync();// Get element by Id// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorvarelement=awaitdevtoolsContext.QuerySelectorAsync("#myElementId");//Set innerText property for the elementawaitelement.SetInnerTextAsync("Welcome!");//Get innerText property for the elementvarinnerText=awaitelement.GetInnerTextAsync();
Html Element classes
There are a number of common HtmlElement implementations that have strongly typed class implementations. A number of common methods have been mapped and will appear as Get/Set[MethodName]Async methods.
//Strongly typed element types//Only a subset of element types have been added so far, use HtmlElement as a generic type for all othersvarhtmlDivElement=awaitdevToolsContext.QuerySelectorAsync<HtmlDivElement>("#myDivElementId");varhtmlSpanElement=awaitdevToolsContext.QuerySelectorAsync<HtmlSpanElement>("#mySpanElementId");varhtmlSelectElement=awaitdevToolsContext.QuerySelectorAsync<HtmlSelectElement>("#mySelectElementId");varhtmlInputElement=awaitdevToolsContext.QuerySelectorAsync<HtmlInputElement>("#myInputElementId");varhtmlFormElement=awaitdevToolsContext.QuerySelectorAsync<HtmlFormElement>("#myFormElementId");varhtmlAnchorElement=awaitdevToolsContext.QuerySelectorAsync<HtmlAnchorElement>("#myAnchorElementId");varhtmlImageElement=awaitdevToolsContext.QuerySelectorAsync<HtmlImageElement>("#myImageElementId");varhtmlTextAreaElement=awaitdevToolsContext.QuerySelectorAsync<HtmlImageElement>("#myTextAreaElementId");varhtmlButtonElement=awaitdevToolsContext.QuerySelectorAsync<HtmlButtonElement>("#myButtonElementId");varhtmlParagraphElement=awaitdevToolsContext.QuerySelectorAsync<HtmlParagraphElement>("#myParagraphElementId");varhtmlTableElement=awaitdevToolsContext.QuerySelectorAsync<HtmlTableElement>("#myTableElementId");
For any other property that isn't mapped they can be accessed via HtmlElement.SetPropertyValueAsync/HtmlElement.GetPropertyValueAsync methods.
Html Table Elements
Table support was added in version 2.0.2
//Get all table elementsvartableElements=awaitdevtoolsContext.QuerySelectorAllAsync<HtmlTableElement>("table");vartable=tableElements[0];varbody=awaittable.GetBodyAsync();varrows=awaitbody.GetRowsAsync().ToArrayAsync();foreach(varrowinrows){varcells=awaitrow.GetCellsAsync().ToArrayAsync();foreach(varcellincells){awaittable.SetInnerHtmlAsync("Updated Div innerText");}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
For those sponsoring the project I'm happy to provide guidance in porting from the
IE WebBrowser
.IMPORTANT
In the interest of getting some real world code examples the first three people to provide code examples I'll provide guidance without change.
If you are a sponsor and require assistance please post your existing code here. For those not currently sponsoring I can provide code examples starting from $50USD via a One Time GitHub sponsorship. Please post your code first and then ask how much to provide a detailed example.
Some general notes and comments are below:
Threading
The
API
is designed to beAsync
and should be used with async all the way. Current limitation ofWebView2
is thatDevTools
protocol method calls can only happen on theUI Thread
(Issue #2) so writingsync
code will deadlock very very easily and should be avoided at all costs.Usage Examples
More detailed usage examples are available at https://github.com/ChromiumDotNet/WebView2.DevTools.Dom#usage
GetElementById
GetElementsByTagName
Element Click
Use the
HtmlElement.ClickAsync()
method to click a button. This method uses mouse emulation, moves the mouse over the element and sends down/up events.Use the
HtmlElement.InvokeMember()
method to click a button. This method calls the HTMLElement.click method.InnerHtml/InnerText
HtmlElement.SetPropertyValueAsync/HtmlElement.GetPropertyValueAsync
can be used to get properties of a HtmlElementHtml Element classes
There are a number of common
HtmlElement
implementations that have strongly typed class implementations. A number of common methods have been mapped and will appear asGet/Set[MethodName]Async
methods.For any other property that isn't mapped they can be accessed via
HtmlElement.SetPropertyValueAsync/HtmlElement.GetPropertyValueAsync
methods.Html Table Elements
Table support was added in version 2.0.2
Beta Was this translation helpful? Give feedback.
All reactions