.Net如何获取Webbrowser中单击元素的ID

我想在Webbrowser中获取所单击元素的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对象:

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