3

I want to get the HTML Id of the clicked Element in a Webbrowser.

Example: If i click the Google Search button it should give me the HTML ID of the clicked element (in this case a button)

How should i achieve that ?

Edit: Webbrowser = The Web browser Control

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
IchSkill
  • 35
  • 1
  • 6

1 Answers1

3

If it's for a Web browser control, then this article explains how to do it: https://www.codeproject.com/Articles/32279/How-To-Tell-What-is-Clicked-in-a-WebBrowser-Contro

First off, we need to translate the mouse coordinates on the screen, into a Point object:

Point ScreenCoord = new Point(MousePosition.X, MousePosition.Y); 

Now, we must create the coordinates of the browser, based off the coordinates of the screen:

Point BrowserCoord = webBrowser1.PointToClient(ScreenCoord);

Now we can use the WebBrowser documents  GetElementFromPoint method to retrieve the element that has been clicked:

HtmlElement elem = webBrowser1.Document.GetElementFromPoint(BrowserCoord);

Now, we can use this element to see what has been clicked:

switch (elem.TagName) { 
case "A": //! We have clicked a link 
break; 
case "IMG": //! We have clicked an image
break; 
default: //! This is anywhere else 
break; 
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321