是否有可能解决没有 ID 的网站上的元素? [英] Is there a possibility to address elements on a website which have no ID?

查看:27
本文介绍了是否有可能解决没有 ID 的网站上的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Vb.net 中,使用 Webbrowser,我通常使用 GetElementById 来寻址例如按钮.我知道有 GetElementFromPoint,我觉得这非常费力.

In Vb.net, with a Webbrowser, I normally use GetElementByIdto address for example a button. I know that there is GetElementFromPoint, which I find extremly laborious.

当 ID 未知时,有没有更好、更简单的方法?

Is there a better, simpler way when the ID is unknown?

推荐答案

您将需要使用某种类型的选择器.

You will need to use some type of selector.

GetElementByID 方法效果最好,因为如果 HTML 文件的格式正确,那么应该只有一个具有该唯一 ID 的元素.

The GetElementByID method works best because if the HTML file is formatted correctly then there should only be one element with that unique ID.

GetElementFromPoint 将根据文档的 X、Y 坐标返回一个元素,最好在文档的 Click 事件中使用.

The GetElementFromPoint will return an element based on the X,Y coordinate of the document, this is best used in the Document's Click event.

GetElementByTagName name 将返回元素集合,如果您知道元素的标签类型,例如 <button>...</button><p>...</p>.为了帮助缩小所需元素的范围,您需要遍历返回的集合并比较元素的属性(如果您知道它们各自的值)或元素的文本(通过 InnerHTML 属性).

The GetElementByTagName name will return a collection of elements and works if you know the tag type of the element, such as <button>...</button> or <p>...</p>. To help narrow down which element you want, you will need to then iterate through the returned collection and compare either the element's attributes if you know their respective values or the element's text via the InnerHTML property.

最后也是最不有效的方法是 All 属性,它返回文档中的每个元素.之所以这样效率最低,是因为至少有了GetElementByTagName,你可以根据标签的名称缩小集合的范围.

The last and least effective method is the All property which returns every element in the document. The reason why this is the least effective is because at least with GetElementByTagName, you can narrow down the collection based on the tag's name.

但是,让我们假设您有以下标记:

However, let's assume that you have the following markup:

<body>
  <p>Look at my super complex HTML markup.</p>
  <button>Click Me</button>
  <button>No, click me!</button>
</body>

然后,您可以使用以下命令获取显示点击我"的按钮标签:

You could then get the button tag that says "Click Me" by using the following:

Dim click_me As HtmlElement = WebBrowser1.Document.GetElementByTagName("button").SingleOrDefault(Function(e) e.InnerHtml = "Click Me")

这篇关于是否有可能解决没有 ID 的网站上的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆