1

I have in a website a line like this:

<!--<td class="points">100.000</td>-->

How can I take that "100.000" and put it in a string? I tried something like this in c#:

string points = driver.FindElement(By.XPath("/html/body/div[2]/div[2]/table[1]/tbody/tr[2]/comment()")).Text;

But I get an error like this when I try to take that number and put in the string:

"The result of the xpath expression "/html/body/div[2]/div[2]/table[1]/tbody/tr[2]/comment()" is: [object Comment]. It should be an element."

Any ideas to do this?

eLRuLL
  • 18,488
  • 9
  • 73
  • 99

1 Answers1

1

No, you cannot directly target comments with FindElement() - it will only work for actual elements - not the text or comment nodes.

You need another way. The idea is to first either get the complete source of the page using driver.PageSource or to get the inner or outer HTML of an element that contains the comment node using elm.GetAttribute("innerHTML") or elm.GetAttribute("outerHTML") where elm could defined in your case as the row containing the comment node inside:

IWebElement elm = driver.FindElement(By.XPath("/html/body/div[2]/div[2]/table[1]/tbody/tr[2]"));

Then, you have several options for how to extract the comment from an HTML string. The best, I think, is to use an HTML parser, like HTML Agility Pack:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • the idea is that: i have a table with 10 rows with points! All the points are commented out. The class for all ten lines with points is "points", and the value is changed every minute... so my only hope is xpath –  Dec 25 '17 at 19:20
  • @MihaiCojocaru nono, not the only hope. Get the inner or outer HTML of the table and HTML parse it extracting the points with HTML agility pack. – alecxe Dec 25 '17 at 19:21
  • can you help me with a piece of code? Because is the first time when i use HAP and i don't understand so good your way to do this job done –  Dec 25 '17 at 19:27
  • @MihaiCojocaru unfortunately, don't have a quick way to set up myself for C#. :) – alecxe Dec 25 '17 at 19:41