.Net How to get the ID of the clicked Element in a Webbrowser

我们两清 提交于 2019-12-07 18:14:26

问题


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


回答1:


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; 
}


来源:https://stackoverflow.com/questions/41293979/net-how-to-get-the-id-of-the-clicked-element-in-a-webbrowser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!