我遇到了麻烦:
我有我公司的内部 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);
.......
关于c# - 将 IwebElement 传递到 html 敏捷包 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31956188/