简体   繁体   English

WebView2 - 获取属性

[英]WebView2 - Get an attribute

I am trying to understand the architecture in WebView2.我正在尝试了解 WebView2 中的体系结构。 With WebBrowser i used to get an attribute from a result returned by the GetElementById as follows: Document.GetElementById("DropDownList").GetAttribute("selectedIndex")使用 WebBrowser,我曾经从 GetElementById 返回的结果中获取属性,如下所示: Document.GetElementById("DropDownList").GetAttribute("selectedIndex")

I know that the ExecuteScriptAsync in WebView2 can run a javascript and return a result as a string.我知道 WebView2 中的ExecuteScriptAsync可以运行 javascript 并将结果作为字符串返回。 However, it looks like that it does not know how to get an attribute from an element.但是,它看起来不知道如何从元素中获取属性。 The below code returns a null.以下代码返回 null。 Although, the getElementById returns the correct result.虽然, getElementById 返回正确的结果。
ExecuteScriptAsync("document.getElementById('DropDownList').getAttribute('selectedIndex')")

Is my syntax incorrect?我的语法不正确吗? How to get an attribute in WebView2?.如何在 WebView2 中获取属性? Do we have to write a function in the script and call it from the host?我们是否必须在脚本中写一个 function 并从主机调用它?

Thanks谢谢

A HTMLSelectElement does not have an attribute called.'selectedIndex'. HTMLSelectElement没有名为“selectedIndex”的属性。 It has a PROPERTY called 'selectedIndex'.它有一个名为“selectedIndex”的属性。

Call it like this from WebView2 :WebView2中这样调用它:

await ExecuteScriptAsync("document.getElementById('DropDownList').selectedIndex");

You need to await this method.您需要等待此方法。 see the reference 见参考

string res = await ExecuteScriptAsync("document.getElementById('DropDownList').getAttribute('selectedIndex')")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM