How to retrieve text from [object Comment]

71 views
Skip to first unread message

Debanjan Bhattacharjee

unread,
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.

Regards
DebanjanB

Vivek Mahajan

unread,
Dec 26, 2017, 12:41:30 PM12/26/17
to webd...@googlegroups.com
Hi Debanjan,

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

Thanks
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 webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at https://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

darrell grainger

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

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.

Darrell

Debanjan Bhattacharjee

unread,
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.

Regards
-DebanjanB

Debanjan Bhattacharjee

unread,
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.

Regards
-DebanjanB
Reply all
Reply to author
Forward
0 new messages