int index=0;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)//是否完全加載完成 { textBox2.AppendText("就緒.....\r\n"); if (index == 0) { if (e.Url.ToString() == webBrowser1.Url.ToString()) { this.cobURL.Text = this.webBrowser1.Document.Url.ToString(); this.TabText.Text = this.webBrowser1.DocumentTitle; HtmlDocument hd = webBrowser1.Document; HtmlElement he = hd.GetElementsByTagName("input").GetElementsByName("keywords")[0]; he.InnerText = this.textBox6.Text.Trim().ToString();//你要查的內容 HtmlElement bt = webBrowser1.Document.All["header-search-button"]; bt.InvokeMember("click");//主頁面,觸發點擊事件,根據你要查的內容去進入子頁面 textBox2.AppendText("正在進入子頁面.....\r\n"); index++; } } else if (index == 1) { textBox2.AppendText("進入"+ e.Url.ToString() + "\r\n"); if (e.Url.ToString() == webBrowser1.Url.ToString()) { this.cobURL.Text = this.webBrowser1.Document.Url.ToString(); this.TabText.Text = this.webBrowser1.DocumentTitle; HtmlDocument hd = webBrowser1.Document; if (hd.GetElementById("DatasheetsTable1")==null) { textBox2.AppendText("該地址未能找到所需數據.....\r\n"); textBox2.AppendText("獲取結束.....\r\n"); webBrowser1.DocumentCompleted -= new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted); index = 0; return; } string ceshi= hd.GetElementById("DatasheetsTable1").FirstChild.OuterText.ToString(); string[] sArray = ceshi.Split(new string[] { "\r\n"}, StringSplitOptions.RemoveEmptyEntries); string bz= sArray[1].Replace("c數據表頭", "");//根據情況處理 bz= bz.Trim().ToString();//獲取數據c textBox3.Text = bz; string ceshi1 = hd.GetElementById("SpecificationTable1").FirstChild.OuterText.ToString(); string[] sArray1 = ceshi1.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); string fz = sArray1[7].Replace("a數據表頭", ""); fz = fz.Trim().ToString();//獲取數據a textBox4.Text = fz; string qjfz = sArray1[8].Replace("b數據表頭", ""); qjfz = qjfz.Trim().ToString();//獲取數據b textBox5.Text = qjfz; index = 0; textBox2.AppendText("獲取結束.....\r\n"); webBrowser1.DocumentCompleted -= new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted); } } } }
private void button7_Click(object sender, EventArgs e) { if (textBox6.Text.ToString()!="") { textBox2.Clear(); textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox2.AppendText("正在獲取中.....\r\n"); this.webBrowser1.Url = new Uri("地址"); this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted); } }
界面大致,webBrowser隱藏了,把webBrowser的ScriptErrorsSuppressed要設置為True,負責會彈script錯誤