WebBrowser如何获得网页中某段HTML代码控件里面的值?

Honyun100 2016-04-28 09:32:23
话不多说,先贴代码。
我WebBrowser获取到一段网页上的HTML代码。

//得到当前表格数据的行数
IList<HtmlElement> rowHtmlElements = webBrowser.Document.GetElementsByTagName("tr").Cast<HtmlElement>().Where(f => f.GetAttribute("ClassName") == "analysis-item" || f.GetAttribute("ClassName") == "analysis-item odd").ToList();
foreach (HtmlElement rowHtmlElement in rowHtmlElements)
{
//得到当前表格的列
IList<HtmlElement> columnHtmlElements = rowHtmlElement.GetElementsByTagName("td").Cast<HtmlElement>().ToList();
foreach (HtmlElement columnHtmlElement in columnHtmlElements)
{

}
}

这个是HTML代码,是从网页上面获取到的HTML代码。

<td style="width: 23.5px; height: 29.5px;" colspan="2">
<input name="spdata[][id]" tabindex="200" class="id" id="spdata__id" type="hidden" value="11968481">
<span class="idx" style="display: inline;">1、</span>
</td>
<td align="left" style="width: 160.5px; height: 30.5px;" colspan="5">
<input name="spdata[][spdata_0]" tabindex="201" title="二氧化硫残留量" class="JYXM inputText" style="width: 160px;" readonly="readonly" value="二氧化硫残留量">
</td>
<td align="left" style="width: 100.5px; height: 31.5px;" colspan="3">
<span><input name="spdata[][spdata_1]" tabindex="202" title="" class="JYJG inputText" style="width: 100px;" value=""></span>
</td>

现在我通过代码

IList<HtmlElement> columnHtmlElements = rowHtmlElement.GetElementsByTagName("td").Cast<HtmlElement>().ToList();
foreach (HtmlElement columnHtmlElement in columnHtmlElements)
{

}

能获取到

<input name="spdata[][spdata_0]" tabindex="201" title="二氧化硫残留量" class="JYXM inputText" style="width: 160px;" readonly="readonly" value="二氧化硫残留量">

我现在要获取到“二氧化硫残留量”这个值,并且我要重新设置<input name="spdata[][spdata_1]" tabindex="202" title="" class="JYJG inputText" style="width: 100px;" value="">这个空间的值。
在下愚笨,想了一下午没想出来。有没有大神help me!!!
...全文
202 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Honyun100 2016-04-29
  • 打赏
  • 举报
回复
引用 4 楼 FoxDave 的回复:
节点取出来再拿一下属性就行了啊
具体怎么取啊?我现在是这样的。

//得到当前表格数据的行数
            IList<HtmlElement> rowHtmlElements = webBrowser.Document.GetElementsByTagName("tr").Cast<HtmlElement>().Where(f => f.GetAttribute("ClassName") == "analysis-item" || f.GetAttribute("ClassName") == "analysis-item odd").ToList();
            foreach (HtmlElement rowHtmlElement in rowHtmlElements)
            {
                //得到当前表格的列
                IList<HtmlElement> columnHtmlElements = rowHtmlElement.GetElementsByTagName("td").Cast<HtmlElement>().ToList();
                foreach (HtmlElement columnHtmlElement in columnHtmlElements)
                {
                    if (columnHtmlElement.GetElementsByTagName("span").Count > 0)
                    {
                        foreach (HtmlElement item in columnHtmlElement.GetElementsByTagName("span"))
                        {
                            if (item.GetElementsByTagName("input").Count > 0)
                            {
                                switch (item.All[0].GetAttribute("ClassName"))
                                {
                                    case "JYJG inputText":
                                        item.All[0].SetAttribute("value", "ss");
                                        break;
                                    case "JGDW inputText":
                                        item.All[0].SetAttribute("value", "ss");
                                        break;
                                    default:
                                        break;
                                }
                            }
                            else if (item.GetElementsByTagName("select").Count > 0)
                            {
                                switch (item.All[0].GetAttribute("ClassName"))
                                {
                                    case "jy_select JGPD":
                                        item.All[0].SetAttribute("value", "不合格项");
                                        item.All[0].SetAttribute("title", "不合格项");
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }
                    else
                    {
                        var xx = columnHtmlElement.All[0].GetAttribute("value");
                    }
                }
            }
有没有更好的办法呢?主要是HtmlElement 类没有好多HtmlDocument的属性和方法。
Honyun100 2016-04-29
  • 打赏
  • 举报
回复
引用 2 楼 rocmemory 的回复:
columnHtmlElement.Arributes["title"] 或者 columnHtmlElement.GetArributeByName("title")
HtmlElement对象里面没有这个属性和方法。我现在用columnHtmlElement.All[0].GetAttribute()或者SetAttribute()能设置值和查找到值。
Justin-Liu 2016-04-29
  • 打赏
  • 举报
回复
节点取出来再拿一下属性就行了啊
为轮子而生 2016-04-29
  • 打赏
  • 举报
回复
修改值可以用SetAttribute
为轮子而生 2016-04-29
  • 打赏
  • 举报
回复
columnHtmlElement.Arributes["title"] 或者 columnHtmlElement.GetArributeByName("title")
秋的红果实 2016-04-28
  • 打赏
  • 举报
回复
既然找到了这个元素,用innerText属性就可以读取或设置其值

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧