.Net 如何在 Webbrowser 中获取被点击元素的 ID

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

我想获取 Web 浏览器中单击元素的 HTML ID。

示例: 如果我单击 Google 搜索按钮,它应该给我所单击元素的 HTML ID(在本例中是一个按钮)

我该如何实现?

编辑:Webbrowser = Web 浏览器控件

如果它是用于 Web 浏览器控件,那么本文将说明如何操作:https://www.codeproject.com/Articles/32279/How-To-Tell-What-is-Clicked-in-a-WebBrowser-Contro

首先,我们需要将屏幕上的鼠标坐标转换为点对象:

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

现在,我们必须根据屏幕坐标创建浏览器坐标:

Point BrowserCoord = webBrowser1.PointToClient(ScreenCoord);

现在我们可以使用 WebBrowser 文档 GetElementFromPoint 方法来检索被点击的元素:

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

现在,我们可以使用这个元素来查看点击了什么:

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