How to retrieve text from [object Comment]

Skip to first unread message

Debanjan Bhattacharjee

Dec 26, 2017, 12:36:41 PM12/26/17
to webdriver
Hello Everyone,

I have an [object Comment] as follows :

<!--<td class="points">100.000</td>-->
I tried to extract "100.000" with the following :

String my_text = driver.findElement(By.xpath("proper_xpath")).getText();
String my_string = driver.findElement(By.xpath("proper_xpath")).getAttribute("innerHTML");
However both of the results are Failing with an error as :

     "The result of the xpath expression "proper_xpath" is: [object Comment]. It should be an element."

Can someone suggest me any other options/alternatives please?

Thanks in advance.


Vivek Mahajan

Dec 26, 2017, 12:41:30 PM12/26/17
Hi Debanjan,

Why can’t you try get pagesource  and then parse string ?

Vivek M

You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
Visit this group at
For more options, visit

darrell grainger

Dec 29, 2017, 12:41:44 PM12/29/17
to webdriver

Selenium parses the DOM. The DOM is similar to the HTML but they are not 100% the same. As you have found, things like comments, <!-- -->, cannot be parsed by Selenium because comments are not stored in the DOM.

As Vivek suggests, you can get the HTML by using the getPageSource() method then parse the HTML to find the comment. Getting the page source will return a String. That String will be HTML. You can then rely on your knowledge of the programming language libraries (nothing to do with Selenium) to parse the HTML and find the comment. You'll need to find a library class which will take as input a String and be able to parse the HTML. You might need to treat the HTML as XML or SGML.


Debanjan Bhattacharjee

Jan 2, 2018, 6:16:23 AM1/2/18
to webdriver
Thanks Vivek,

That was a gem of an idea. Worked smooth. Thanks for the insight.


Debanjan Bhattacharjee

Jan 2, 2018, 6:19:08 AM1/2/18
to webdriver
Hi Darrell,

Thanks for affirming the approach. The approach did work well. Thanks for the insight. Python & Java both ways I just had to `split()` the String.

Reply all
Reply to author
0 new messages