询问者
HtmlElement的FirstChild属性在遇到有特定内容的子元素时会返回null,为什么?

问题
-
.net framework 3.5,使用webBrowser控件时发现以下问题
HtmlElement类有一个属性FirstChild,查找当前html元素下的第一个子元素,一般情况下毫无问题,但是请看下面这段html代码
<div class="dib tl"> <p>编辑短信:<span class="f20">ub61</span></p> </div>
我首先检索到了dib tl的div,通过FirstChild可以搜索到p元素,然后我在得到了p元素的HtmlElement后,我想进一步得到span元素,结果这个时候返回的却是null
HtmlElementCollection divHtmlElementCollection = this.webBrowser1.Document.GetElementsByTagName("div"); foreach (HtmlElement he in divHtmlElementCollection) { if (he.GetAttribute("className").Equals("dib tl")) { HtmlElement heP = he.FirstChild; Console.WriteLine("找到:" + heP .OuterHtml); HtmlElement heSoan = heP.FirstChild;//得到的是null //Console.WriteLine("找到:" + heP .OuterHtml); break; } }
我后来又尝试的了Children[0],结果这次正常了,能够正常获取到span元素
,后来我反复研究了一下,发现,原来问题在于p和span之间有“编辑短信”这几个文字,只要删掉变成下面这样
<p><span class="f20">ub61</span></p>
FirstChild属性就能找到span元素了,但是我不理解这是为什么,这是bug还是别的原因?
- 已移动 Sheng Jiang 蒋晟Moderator 2015年7月19日 15:47 WinForms问题
全部回复
-
你好,
可能是对中文字符的识别的问题,具体我还没看,是不是纯英文的就能读出来?你有没有看看debug的时候的结果是什么?
Barry
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.