/// <summary> /// Dom单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void On_DomClick(object sender, DomMouseEventArgs e) { //屏蔽页面点击事件 e.PreventDefault(); e.StopPropagation(); var ele = e.CurrentTarget.CastToGeckoElement(); ele = e.Target.CastToGeckoElement(); GeckoHtmlElement geckoHtmlElement = (GeckoHtmlElement)ele; string searchElement = geckoHtmlElement.OuterHtml; if (!searchElement.Contains("firefinder-match-red")) { //在datagridview中显示选中行的内容 GeckoHtmlElement[] geckoHtmlEle = new GeckoHtmlElement[1]; geckoHtmlEle[0] = geckoHtmlElement; if (geckofxType.Equals(GeckofxWebbrowerType.General)) { InsertDataGridRow(dgv, geckoWebBrowser, geckoHtmlEle); } else if (geckofxType.Equals(GeckofxWebbrowerType.ListDetails)) { InsertDataGridRow(dgv, geckoWebBrowser, geckoHtmlEle); } else if (geckofxType.Equals(GeckofxWebbrowerType.UrlModel)) { try { GeckoAnchorElement d = (GeckoAnchorElement)geckoHtmlElement; string URL = d.Href; InsertDataGridRow(GeckofxWebbrowerType.UrlModel, dgv, geckoWebBrowser, geckoHtmlEle); } catch (Exception) { MessageBox.Show("请获取链接!"); return; } } ruleStyle.AddClass("firefinder-match-red", ele); } else { //在浏览器中标记红圈 ruleStyle.RemoveClass("firefinder-match-red", ele); } }
public void InsertDataGridRow(GeckofxWebbrowerType geckofxWebbrowerType, DataGridView dgv, GeckoWebBrowser geckoWebBrowser, params GeckoElement[] ele) { List <GeckoHtmlElement> geckoHtmlElementList = null; //设置xpath 获取html信息 if (ele.Length == 1) { var xpathTxt = xpathHelper.GetSmallXpath(ele[0]); var xresult = geckoWebBrowser.DomDocument.EvaluateXPath(xpathTxt); var nodes = xresult.GetNodes(); var elements = nodes.Select(x => x as GeckoElement).ToArray(); geckoHtmlElementList = XpathHelper.FindHtmlTxt(true, elements); } else { geckoHtmlElementList = XpathHelper.FindHtmlTxt(true, ele); } if (geckofxWebbrowerType.Equals(GeckofxWebbrowerType.General)) { foreach (GeckoHtmlElement element in geckoHtmlElementList) { DataGridViewRow row = new DataGridViewRow(); //文本 DataGridViewTextBoxCell fieldsNameCell = new DataGridViewTextBoxCell(); fieldsNameCell.Value = "字段" + (count++); row.Cells.Add(fieldsNameCell); DataGridViewTextBoxCell fieldsContentCell = new DataGridViewTextBoxCell(); fieldsContentCell.Value = element.TextContent; row.Cells.Add(fieldsContentCell); //下拉框 DataGridViewTextBoxCell fieldsTypeCell = new DataGridViewTextBoxCell(); fieldsTypeCell.Value = "抓取文本"; row.Cells.Add(fieldsTypeCell); DataGridViewImageCell deleteCell = new DataGridViewImageCell(); deleteCell.Value = Image.FromFile(@"E:\Project\C#\SimpleCrawlProject\SimpleCrawlApp\Resources\1108658.png"); row.Cells.Add(deleteCell); //隐藏浏览器句柄 DataGridViewTextBoxCell visibleGeckofxElementCell = new DataGridViewTextBoxCell(); visibleGeckofxElementCell.Value = element; row.Cells.Add(visibleGeckofxElementCell); //隐藏xpath DataGridViewTextBoxCell visibleGeckofxXpathCell = new DataGridViewTextBoxCell(); visibleGeckofxXpathCell.Value = xpathHelper.GetSmallXpath(element); row.Cells.Add(visibleGeckofxXpathCell); //将元素插入datagridview控件中 dgv.Rows.Add(row); } } else if (geckofxWebbrowerType.Equals(GeckofxWebbrowerType.ListDetails)) { } else if (geckofxWebbrowerType.Equals(GeckofxWebbrowerType.UrlModel)) { foreach (GeckoHtmlElement element in geckoHtmlElementList) { DataGridViewRow row = new DataGridViewRow(); //文本 DataGridViewTextBoxCell fieldsNameCell = new DataGridViewTextBoxCell(); fieldsNameCell.Value = "字段" + (count++); row.Cells.Add(fieldsNameCell); DataGridViewTextBoxCell fieldsContentCell = new DataGridViewTextBoxCell(); fieldsContentCell.Value = element.TextContent; row.Cells.Add(fieldsContentCell); //下拉框 DataGridViewTextBoxCell fieldsTypeCell = new DataGridViewTextBoxCell(); fieldsTypeCell.Value = "抓取详情页链接"; row.Cells.Add(fieldsTypeCell); DataGridViewImageCell deleteCell = new DataGridViewImageCell(); deleteCell.Value = Image.FromFile(@"E:\Project\C#\SimpleCrawlProject\SimpleCrawlApp\Resources\1108658.png"); row.Cells.Add(deleteCell); //隐藏浏览器句柄 DataGridViewTextBoxCell visibleGeckofxElementCell = new DataGridViewTextBoxCell(); visibleGeckofxElementCell.Value = element; row.Cells.Add(visibleGeckofxElementCell); //隐藏xpath DataGridViewTextBoxCell visibleGeckofxXpathCell = new DataGridViewTextBoxCell(); visibleGeckofxXpathCell.Value = xpathHelper.GetSmallXpath(element); row.Cells.Add(visibleGeckofxXpathCell); //将元素插入datagridview控件中 dgv.Rows.Add(row); } } }