2

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.

Community
  • 1
  • 1
Ave
  • 4,338
  • 4
  • 40
  • 67
  • 1
    Are you using HtmlAgility Pack correctly as they are using it for HtmlDocument class. – Adil Jun 16 '16 at 04:54
  • 2
    Try this : `Install-Package HtmlAgilityPack` – sujith karivelil Jun 16 '16 at 04:55
  • In pre-comment suggest using `HtmlAgilityPack`. So I think `HtmlDocument` in namespace `System.Windows.Web`. I will try with `HtmlAgility Pack`. – Ave Jun 16 '16 at 04:57
  • Thanks. When to install this package. It's working. But in question, I have a problem like LoadHtml only accept string HTML. But I want to add rows: `IList tableRows = browser.FindElementsByXPath("id('column2')/tbody/tr");` to this. Have any method to do this? Thanks – Ave Jun 16 '16 at 05:01
  • why not continue with selenium ???, for what reason you want to use agility pack ? – Leon Barkan Jun 16 '16 at 07:09
  • When I tried scan table about ~1000 rows, it throws exception like: `Stale element reference: element is not attached to the page document.` – Ave Jun 16 '16 at 07:36

1 Answers1

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

Leon Barkan
  • 2,676
  • 2
  • 19
  • 43