/// <summary> /// 解析表结构 /// </summary> protected void ParseStructure() { if (!EnvManager.Auto) { return; } if (table == null) { table = new DataTable(); } //var cols = SolutionLocator.LocateElements(new List<By> { By.CssSelector("table thead tr th") }); var cols = SolutionLocator.LocateElements(new List <By> { By.CssSelector("table thead tr th") }); foreach (var col in cols) { string colName = col.Text; if (colName.NullOrWhiteSpace()) { table.Columns.Add(Guid.NewGuid().ToString()); // 列头名称为空,使用自己字符串占位 } else { table.Columns.Add(colName, typeof(string)); } } }
/// <summary> /// 获取当前页的数据 /// </summary> /// <returns></returns> public virtual DataTable GetPageData() { if (!EnvManager.Auto) { return(null); // 如果非自动化运行,应该返回null,以防止出现异常 } if (table == null) { ParseStructure(); } table.Clear(); var rows = SolutionLocator.LocateElements(new By[] { By.CssSelector("table tbody tr") }); foreach (var row in rows) { DataRow dr = table.NewRow(); var cols = row.FindElements(By.CssSelector("td")); int i = 0; List <string> colValues = new List <string>(); foreach (var col in cols) { string colValue = col.Text; colValues.Add(colValue); i++; } dr.ItemArray = colValues.ToArray(); table.Rows.Add(dr); } return(table); }