问题
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