使用Web浏览器控件按类名获取div的内容? [英] Get the content of a div by class name using Web Browser control?

查看:34
本文介绍了使用Web浏览器控件按类名获取div的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 webBrowser1 控件的表单,该表单用于加载HTML行中包含以下行的页面:

I have a form with webBrowser1 control that is used to load a page that contains in its HTML part the following line:

 ...
 <div class="cls">
 Hello World !
 </div>

我需要获取 div 元素的 innerText .我尝试了以下方法:

I need to get the innerText of the div element. I tried the following:

 string result = "";
 foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div"))
                if (el.GetAttribute("class") == "cls")
                {
                    result = el.InnerText;
                }

但是上面的代码似乎不起作用,并且根据 表示,

But the code above doesn't seem to work, and according to Solution 1 for a similar question on another website, it is stated that

引起我注意的第一件事是:< div class ="thin"> 没有定义 div 元素的名称,它定义了CSS样式班级.而是使用属性名称(或同时使用两者),例如:< div class ="thin" name ="thin"> .

First thing which caught my eye is: <div class="thin"> does not define the name of the div element, it defines its CSS style class. Instead, use the attribute name (or both), for example: <div class="thin" name="thin">.

如果仅具有 class 属性,如何获取 div 元素的 innerText ?

How can I get the innerText of the div element, if there is a class attribute only?

任何帮助将不胜感激.

推荐答案

您应使用 ClassName 而不是 class :

 if (el.GetAttribute("className") == "cls") { … }

有关详细信息,请参见.

For details, see HtmlElement.GetAttribute("class") doesn't run.

这篇关于使用Web浏览器控件按类名获取div的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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