简体   繁体   中英

How to get the clicked element using webview2 in windows forms

I have a query in windows. forms I am new to this.

I have developed a form where users can open any website from it and upon right click of any element I am displaying the element name, id, and few attribute values in a data grid. For this, I have used webbrowser control.

However, I was facing some errors for a few of the sites so I tried to move to webview2. But here comes the issue

Earlier I used to get the element using the below code

HtmlElement element = webbrowser1.Document.GetElementFromPoint(e.ClientMousePosition);

But now I am unable to retrieve an element by using webview2.

Can someone please help me with this?

You will need to use JavaScript. You will need async methods. Then, you can get the element by passing a javeScript string.

Point p = e.ClientMousePosition;
string jElement = await webBrowser1.ExecuteScriptAsync($"document.elementFromPoint({p.X},{p.Y})");

The result is JSON. You will need to parse the result to get the element name.

I am trying to figure out the same thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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