C# webbrowser如何访问HtmlElement CSS类?

发布于 2024-12-21 12:55:16 字数 416 浏览 0 评论 0原文

我正在尝试填写网络表格。我们如何访问html中的HTML元素类?通常我们使用这段代码:

webBrowser2.Document.GetElementById

html如下:

<select class="birthday_day" name="birthday_day" >
    <option value="0">---</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
      </select>

I am trying to fill out a web form. How do we access a class of HTML element in html? Normally we use this code:

webBrowser2.Document.GetElementById

The html is below:

<select class="birthday_day" name="birthday_day" >
    <option value="0">---</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
      </select>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

桃扇骨 2024-12-28 12:55:16

您可能想通过名称而不是类来执行此操作,因此您可以尝试使用 Document.All 属性:

webBrowser2.Document.All["birthday_day"]

You probably want to do it by name, not class, so you could try using the Document.All property:

webBrowser2.Document.All["birthday_day"]
゛时过境迁 2024-12-28 12:55:16

考虑使用 getAttribute - http://msdn.microsoft .com/en-us/library/system.windows.forms.htmlelement.getattribute.aspx CLASS 和 NAME 都应该可用。

Consider using getAttribute - http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.getattribute.aspx both CLASS and NAME should be available.

〃温暖了心ぐ 2024-12-28 12:55:16

老问题,但因为很难找到:
正如前面提到的,GetAttribute 是答案的一部分。技巧是使用正确的属性名称:

var element = webBrowser2.Document.GetElementById("birthday_day");
var classes = element.GetAttribute("className"); // instead of just "class"

这不是 WebBrowser 控件的怪癖,而是 DOM API 从那时起,在 JavaScript 中使用诸如 class 之类的关键字作为属性名称是一个巨大的问题。

Old question, but since it's rather hard to find:
As previously mentioned GetAttribute is part of the answer. The trick is to use the correct attribute name:

var element = webBrowser2.Document.GetElementById("birthday_day");
var classes = element.GetAttribute("className"); // instead of just "class"

This is not a quirk of the WebBrowser control, but legacy of the DOM API from back when it was a huge problem to have keywords such as class as property names in JavaScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文