.Net 如何在浏览器中获取被点击元素的 ID [英] .Net How to get the ID of the clicked Element in a Webbrowser
问题描述
我想在浏览器中获取被点击元素的 HTML Id.
I want to get the HTML Id of the clicked Element in a Webbrowser.
示例:如果我点击 Google 搜索按钮,它应该给我点击元素的 HTML ID(在这种情况下是一个按钮)
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 ?
Webbrowser = Web 浏览器控件
Webbrowser = The Web browser Control
推荐答案
如果是针对 Web 浏览器控件,那么这篇文章解释了如何去做:https://www.codeproject.com/Articles/32279/How-To-Tell-什么是点击的 Web 浏览器控件
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
首先,我们需要将屏幕上的鼠标坐标转换为 Point 对象:
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);
现在我们可以使用 WebBrowser 文档 GetElementFromPoint 方法来检索被点击的元素:
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;
}
这篇关于.Net 如何在浏览器中获取被点击元素的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!