Ave
Ave

Reputation: 4430

Can't using HtmlDocument in C#

I create Console Application using Selenium to get the text from a table.

Tried with code:

IList<IWebElement> tableRows = browser.FindElementsByXPath("id('column2')/tbody/tr");
var doc = new HtmlDocument();
doc.LoadHtml(tableRows);

This error like:

'HtmlDocument' does not contain a constructor that takes 0 arguments

I read this answer from question

Almost people in Stackoverflow can be using like:

new HtmlDocument.

Why I can't be using this. I tried with Winform Application, but I also can't using HtmlDocument.

And HtmlDocument seems only LoadHmtl(String). But my code is IList<IWebElement>.

I don't know how to convert it to HTML string to add to doc.

Upvotes: 2

Views: 2774

Answers (1)

Leon Barkan
Leon Barkan

Reputation: 2703

IWebElement table = browser.FindElement(By.Id("column2");
var doc = new HtmlDocument();
doc.LoadHtml(table.InnerHtml);

first off all you can get the table elements using selenium... , if you chose to use agility pack you need to send to LoadHtml method string variable with html source so what you need to do is to find the html block (in your case is the table) take it as IWebElement and send it to LoadHtml using table.InnerHtml

also you can send the full page source doc.LoadHtml(driver.PageSource);

Upvotes: 1

Related Questions