我遇到了麻烦:
我有我公司的内部HTML。在这个页面中,我有一张桌子。我想在C#中将HTML表转换为DataTable:
这是我的代码:
IWebElement ie = driver.FindElement(By.XPath(".//*[@id='frmClaimSearch']/div[2]/div[1]/div/table"));
ie.GetAttribute("innerHTML");//get Element of the HTML
var doc = new HtmlDocument();
doc.Load("????");
var nodes = doc.DocumentNode.SelectNodes(".//*[@id='frmClaimSearch']/div[2]/div[1]/div/table/tr");
var table = new DataTable("MyTable");
var headers = nodes[0]
.Elements("th")
.Select(th => th.InnerText.Trim());
foreach (var header in headers)
{
table.Columns.Add(header);
}
var rows = nodes.Skip(1).Select(tr => tr
.Elements("td")
.Select(td => td.InnerText.Trim())
.ToArray());
foreach (var row in rows)
{
table.Rows.Add(row);
}
@id='frmClaimSearch']/div[2]/div[1]/div/table/tr");
var table = new DataTable("MyTable is my XPath of Table.
但这些代码行不起作用。我尝试了很多。请帮帮我。非常感谢。
使用LoadHtml()
方法而不是Load()
从HTML字符串填充HtmlDocument
:
IWebElement ie = driver.FindElement(By.XPath(".//*[@id='frmClaimSearch']/div[2]/div[1]/div/table"));
string innerHtml = ie.GetAttribute("innerHTML");
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(innerHTML);
.......