Pass IwebElement into html agility pack C#

c# html-agility-pack selenium

Question

I have in trouble with this:

I have an internal HTML of my company. In this page, I have a table. I want to convert HTML table to DataTable in C#:

Here is my code:

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.

But these codes line do not work. I tried so much. Help me please. Many Thanks.

Popular Answer

Use LoadHtml() method instead of Load() to populate HtmlDocument from HTML string :

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);

.......


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why