將IwebElement傳遞給html敏捷包C#

c# html-agility-pack selenium

我遇到了麻煩:

我有我公司的內部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);

.......


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow