这几天在学做一个通过WebBrowser打开网页获取数据的程序,由于是个新手,期间遇到了很多的问题:如下

一、不知道怎么判断网页完全打开了

      解决方法:

            myWeb.Navigate(strURL);

while (myWeb.ReadyState != WebBrowserReadyState.Complete)//如果页面没有载入完毕,则让程序继续加载
{
Application.DoEvents();
}

HtmlDocument hd
= myWeb.Document;
string str = hd.Body.InnerText;
if (str.IndexOf("用户名:") > -1)
{
this.Invoke(smi, new object[] { "打开登陆页面成功" });
}

  

二、点击网页中的事件(button或者链接)(不是通过WebBrowser.Navigate())后不知道怎么获得新的网页内容

  解决方法:通过WebBrowser控件的DocumentCompleted事件进行获取 如下:

private void myWeb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlDocument hd
= this.myWeb.Document;
HtmlElementCollection hecTD
= hd.Body.GetElementsByTagName("td");
foreach (HtmlElement he in hecTD)
{
if (he.InnerText == txtXueyh.Text.Trim())
{
queryOK
= true;
}
}
}

  

三、获得文本框中的内容获取不了

  解决方法:
           文本框中的内容要不能用innerText 只能用value  比如一个ID为“txtXingming”的文本框,要获得他的内容如下:          

string xingming = hd.GetElementById("txtXingming").GetAttribute("value");

  

四、如何获得下接列表框(select)中的选项数据

  解决方法:先通过select元素的ID获得它的对象,然后再获得它的子元素option,如下:

HtmlElementCollection hecXingbie = hd.GetElementById("drpXingbie").GetElementsByTagName("option");
string xingbie = "";
foreach (HtmlElement he in hecXingbie)
{
string xb = he.GetAttribute("selected");
if ( !string.IsNullOrEmpty(xb) && xb.ToLower()=="true")
{
xingbie
= he.InnerText;
break;
}
}
posted on 2011-09-02 23:02  greensoybean  阅读(448)  评论(0)    收藏  举报