javascript - WebView2 - Get an attribute - Stack Overflow

I am trying to understand the architecture in WebView2. With WebBrowser i used to get an attribute from

I am trying to understand the architecture in WebView2. With WebBrowser i used to get an attribute from a result returned by the GetElementById as follows: Document.GetElementById("DropDownList").GetAttribute("selectedIndex")

I know that the ExecuteScriptAsync in WebView2 can run a javascript and return a result as a string. However, it looks like that it does not know how to get an attribute from an element. The below code returns a null. Although, the getElementById returns the correct result.
ExecuteScriptAsync("document.getElementById('DropDownList').getAttribute('selectedIndex')")

Is my syntax incorrect? How to get an attribute in WebView2?. Do we have to write a function in the script and call it from the host?

Thanks

I am trying to understand the architecture in WebView2. With WebBrowser i used to get an attribute from a result returned by the GetElementById as follows: Document.GetElementById("DropDownList").GetAttribute("selectedIndex")

I know that the ExecuteScriptAsync in WebView2 can run a javascript and return a result as a string. However, it looks like that it does not know how to get an attribute from an element. The below code returns a null. Although, the getElementById returns the correct result.
ExecuteScriptAsync("document.getElementById('DropDownList').getAttribute('selectedIndex')")

Is my syntax incorrect? How to get an attribute in WebView2?. Do we have to write a function in the script and call it from the host?

Thanks

Share Improve this question edited Apr 1, 2021 at 21:17 Poul Bak 10.9k5 gold badges38 silver badges69 bronze badges asked Apr 1, 2021 at 15:09 ZimoZimo 3427 silver badges24 bronze badges 3
  • You JavaScript never worked with Webbrowser either. -1 from me. – darbid Commented Apr 2, 2021 at 12:41
  • @darbid: Actually in the old WebBrowser, you used C# to access the dom, not javascript (the code shown is C#). – Poul Bak Commented Apr 3, 2021 at 18:33
  • I was referring to this “Although, the getElementById returns the correct result.” – darbid Commented Apr 4, 2021 at 7:20
Add a ment  | 

2 Answers 2

Reset to default 5

A HTMLSelectElement does not have an attribute called.'selectedIndex'. It has a PROPERTY called 'selectedIndex'.

Call it like this from WebView2:

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

Many people are confused about javascript attributes versus properties. Simple rule (easy to remember) is:

Attributes can be written directly in the html tag, like 'id', 'name' etc. Properties are set by runtime.

You need to await this method. see the reference

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742350921a4427498.html

相关推荐

  • javascript - WebView2 - Get an attribute - Stack Overflow

    I am trying to understand the architecture in WebView2. With WebBrowser i used to get an attribute from

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信