private void CrawlCurrentPage(WebBrowser wb, bool isOffline, bool IsUnConfirmChecked, ref bool hasValidData) { mshtml.IHTMLDocument2 doc2 = isOffline ? null : (mshtml.IHTMLDocument2)wb.Document; string html = isOffline ? s_htmlFake : doc2.body.innerHTML; Debug.WriteLine(html); List <ImportInvoiceDTO> list = new List <ImportInvoiceDTO>(); List <hParser.Tags.TableRow> validRowList = new List <hParser.Tags.TableRow>(); //this.parseResult = ""; #region 使用IHTMLDocument2提取HTML mshtml.HTMLTableClass table = IsUnConfirmChecked ? (mshtml.HTMLTableClass)doc2.all.item("example1", 0) : (mshtml.HTMLTableClass)doc2.all.item("example", 0); if (table == null) { hasValidData = false; //throw new InvalidOperationException("无效table"); return; } mshtml.HTMLTableSectionClass tbody = (mshtml.HTMLTableSectionClass)table.lastChild; if (tbody == null) { hasValidData = false; //throw new InvalidOperationException("无效tbody"); return; } var tbodyHtml = tbody.innerHTML; if (0 == string.Compare(tbody.innerText, "没找到记录", StringComparison.InvariantCultureIgnoreCase)) { hasValidData = false; //throw new InvalidOperationException("无效tbody"); return; } #region WPF WebBroswer交互源代码DOM元素总结 #if RESEARCH //HTMLDocument doc01 = wb.Document as HTMLDocument; ////IHTMLDocument2 doc02 = wb.Document as IHTMLDocument2; //Debug.WriteLine(doc01.body.innerHTML); ///读/写元素 /// mshtml.IHTMLElement login_pass = (mshtml.IHTMLElement)doc2.all.item("login_pass", 0); mshtml.IHTMLElement password = (mshtml.IHTMLElement)doc2.all.item("password", 0); password.setAttribute("value", "12345678"); login_pass.setAttribute("style", ""); mshtml.IHTMLElement login_pass1 = (mshtml.IHTMLElement)doc2.all.item("login_pass1", 0); mshtml.IHTMLElement password1 = (mshtml.IHTMLElement)doc2.all.item("password1", 0); login_pass1.setAttribute("style", "display:none;"); //password1.setAttribute("style", "width:1px"); //IHTMLElement item = doc01.getElementById("ptmm"); //item.innerHTML = "<INPUT id=\"pwd\" class=\"login_input password\" type=\"text\" value=\"\" />"; //// doc01.body.insertAdjacentHTML(,); //MessageBox.Show(item.innerText); //wb.NavigateToString(doc01.body.innerHTML); /// Trigger event //点击确定按钮 loginBT.click(); /// script injection /// //Basic ds = new Basic(); //wb.ObjectForScripting = ds;//该对象可由显示在WebBrowser控件中的网页所包含的脚本代码访问 ///Levarage JS /// mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow; win.execScript("Login('12345678', '', 1)", "javascript"); return; #endif #endregion #endregion #region 使用HtmlParser提取tbodyHtml Lexer lexer = new Lexer(tbodyHtml); hParser.Parser parser = new hParser.Parser(lexer); hParser.NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.TableRow)); NodeList nodeList = parser.Parse(filter); if (nodeList.Count == 0) { hasValidData = false; MessageBox.Show("没有符合要求的节点"); } else { for (int i = 0; i < nodeList.Count; i++) { //抓取一行 var tagTR = parserTR(nodeList[i]); #region 充填有效行 if (tagTR != null) { validRowList.Add(tagTR); } #endregion } parserValidTR(validRowList, IsUnConfirmChecked, ref list); } #endregion #region 使用HtmlParser提取HTML /* Lexer lexer = new Lexer(html); * hParser.Parser parser = new hParser.Parser(lexer); * hParser.NodeFilter filter = new NodeClassFilter(typeof(Winista.Text.HtmlParser.Tags.TableRow)); * NodeList nodeList = parser.Parse(filter); * if (nodeList.Count == 0) * MessageBox.Show("没有符合要求的节点"); * else * { * for (int i = 0; i < nodeList.Count; i++) * { * //抓取一行 * var tagTR = parserTR(nodeList[i]); * #region 充填有效行 * if (tagTR != null) * validRowList.Add(tagTR); #endregion * * } * * parserValidTR(validRowList, ref list); * * } */ #endregion #region 日志 & 导出 & 持久化 if (list == null || list.Count == 0) { MessageBox.Show("该页面上没有检测到预期数据"); hasValidData = false; } ImportInvoiceListDTO soap = new ImportInvoiceListDTO { List = list, Result = new ImportInvoiceResultDTO { Message = "CALLBACK", Status = 9 } }; Debug.Write(soap); #region Log if (this.IfLog == "1") { soap.List.ForEach(impinfo => { if (IsUnConfirmChecked) { LogHelper.WriteLog(typeof(WebBoxView), string.Format("发票代码{0} 发票号码{1} 开票日期{2} 销方税号{3} 金额{4} 税额{5} 来源{6} 发票状态{7} 勾选标志{8} 操作时间{9}", impinfo.InvoiceCode, impinfo.InvoiceNumber, impinfo.CreateDate, impinfo.SalesTaxNumber, impinfo.Amount, impinfo.Tax, impinfo.From, impinfo.Status, impinfo.SelectTag, impinfo.ChosenTime)); } else { LogHelper.WriteLog(typeof(WebBoxView), string.Format("发票代码{0} 发票号码{1} 开票日期{2} 销方税号{3} 金额{4} 税额{5} 来源{6} 发票状态{7} 确认月份{8}", impinfo.InvoiceCode, impinfo.InvoiceNumber, impinfo.CreateDate, impinfo.SalesTaxNumber, impinfo.Amount, impinfo.Tax, impinfo.From, impinfo.Status, impinfo.SelectTag)); } }); } #endregion if (this.IfCallWS == "1") { CallWS(soap); } Debug.Write("本页已同步完成,请点击下一页继续同步"); //FakeBusy(); #endregion }
public void LoadCompletedHandler(NavigationEventArgs e, object view, WebBrowser wb) { var currentUrl = wb.Source.AbsoluteUri; if (wb == null) { return; } wb.Visibility = Visibility.Visible; var vw = view as WebBoxView; if (vw != null) { ProgressBar pBar = vw.FindName("ProgBar") as ProgressBar; if (pBar != null) { pBar.Visibility = Visibility.Hidden; } } this.IsBusy = false; if (currentUrl.Contains("#")) { currentUrl = currentUrl.Replace("#", ""); } if (currentUrl == this.TargetUri) { this.TipInfo = "加载已完成"; //MessageBox.Show("请先查询所需的进向数据"); // StartCrawl(); } else if (currentUrl != this.StartUri) {//除首页登录页面外,都重定向到fpcx.html wb.Source = new Uri(this.TargetUri); } bool isOffline = string.Compare(this.IsOffline, "1", StringComparison.InvariantCultureIgnoreCase) == 0 ? true : false; mshtml.IHTMLDocument2 doc2 = isOffline ? null : (mshtml.IHTMLDocument2)wb.Document; string html = isOffline ? s_htmlFake : doc2.body.innerHTML; #region 加迷彩 login_header login_footer mshtml.IHTMLElement login_header = (mshtml.IHTMLElement)doc2.all.item("login_header", 0); mshtml.IHTMLElement login_footer = (mshtml.IHTMLElement)doc2.all.item("login_footer", 0); mshtml.IHTMLElement footer = (mshtml.IHTMLElement)doc2.all.item("footer", 0); mshtml.IHTMLElement header = (mshtml.IHTMLElement)doc2.all.item("header", 0); if (login_header != null) { // login_header.setAttribute("style", "display: none;"); login_header.innerHTML = ""; } if (login_footer != null) { // login_header.setAttribute("style", "display: none;"); login_footer.innerHTML = ""; } if (footer != null) { // login_header.setAttribute("style", "display: none;"); footer.innerHTML = ""; } if (header != null) { // login_header.setAttribute("style", "display: none;"); header.innerHTML = ""; } //屏蔽右键 //(wb.Document as mshtml.HTMLDocumentEvents_Event).oncontextmenu += new mshtml.HTMLDocumentEvents_oncontextmenuEventHandler(ExtendFrameControl_oncontextmenu); //wb.ContextMenu = null; //wb.ContextMenuOpening += Wb_ContextMenuOpening; #endregion #region Auto Login if (currentUrl == this.StartUri && !string.IsNullOrEmpty(this.DeviceKey)) { mshtml.IHTMLWindow2 win = (mshtml.IHTMLWindow2)doc2.parentWindow; win.execScript("Login('12345678', '', 1)", "javascript"); return; } #endregion // ShowScreen("", typeof(StartScreenViewModel)); }