How to get the clicked element using webview2 in windows forms

1.3k Views Asked by At

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?

1

There are 1 best solutions below

0
dashrader On

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.