get innerHTML of table cell

647 views
Skip to first unread message

Gaurang shah

unread,
Dec 13, 2012, 12:50:51 PM12/13/12
to webd...@googlegroups.com
Hi, 

I require to get the innerHTML of the table cell, would someone pleaes let me know how to do it. 

<table>
<tbody>
<tr> 
<td><b>gaurang</b></td>
</tr>
</tbody>
</table>

I want "<b>gaurang</b>" text. I tried following way but, it shows empty string 

JS.executeScript("arguments[0].innerHTMML",obj)

Smita Sinha

unread,
Dec 14, 2012, 6:39:33 AM12/14/12
to webd...@googlegroups.com

Have you tried clicking on that element and getText.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webdriver/-/xPK-HcYABB0J.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

SuperKevy

unread,
Dec 14, 2012, 1:01:01 PM12/14/12
to webd...@googlegroups.com
Try searching this group for innerHTML 

 WebElement element = driver.findElement(By.id("foo"));
 String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);

or try
element.getAttribute("innerHTML");

I use ruby so I'm akin to .getAttribute

ASP

unread,
Dec 14, 2012, 2:43:43 PM12/14/12
to webd...@googlegroups.com
you could do something like 

webdriver.findElement(By.xpath("//tr/td/b")).getText();

xpath will change based on ur requirements ... with .getText() you should be able to get text 


ASP

David

unread,
Dec 14, 2012, 6:07:31 PM12/14/12
to webdriver
You never mention what element locator you have used. The method you
provide has typo and syntax error. The code provided by SuperKevy
should work.

As for the correct element locator, you could start with

"//table/tbody/tr/td" as the XPath locator, though you could simplify
it as well, that's the absolute XPath to start with.

Or with CSS selectors, "table > tbody > tr > td" as the abolute CSS
selector to start with, which could be simplified too.

I wouldn't recommend getText(), unless the requirement was just to get
the text "gaurang". If the requirement was to check the text with HTML
formatting, then innerHTML (or page source) is better approach. Though
what ASP provides for sample code does indirectly check for the HTML
formatting via defining the locator (rather than in the text value).

Bill Ross

unread,
Dec 15, 2012, 1:43:33 PM12/15/12
to webd...@googlegroups.com
David <mang...@gmail.com> wrote:
>
>...
> As for the correct element locator, you could start with
>
> "//table/tbody/tr/td" as the XPath locator, though you could simplify
> it as well, that's the absolute XPath to start with.

If you have multiple tables including tables within tables, this
is more complicated. It's great if each table has an id, or at least
a signature parentage for xpath. Starting with the ideal, and
moving in increasing order of detestability:

"//table[@id='foo']/tbody/tr/td"
"//div[@id='foo']/table/tbody/tr/td"
"//div/span[2]/div/table/tbody/tr/td"

And finally, the embedded table:

"//div/span[2]/div/table/tbody/tr/td[3]/table/tbody/tr[3]/td[2]"

> Or with CSS selectors, "table > tbody > tr > td" as the abolute CSS
> selector to start with, which could be simplified too.

Not sure how CSS would handle embedded tables.

Bill

>
> I wouldn't recommend getText(), unless the requirement was just to get
> the text "gaurang". If the requirement was to check the text with HTML
> formatting, then innerHTML (or page source) is better approach. Though
> what ASP provides for sample code does indirectly check for the HTML
> formatting via defining the locator (rather than in the text value).
>
> On Dec 13, 9:50�am, Gaurang shah <gaurangns...@gmail.com> wrote:
> > Hi,
> >
> > I require to get the innerHTML of the table cell, would someone pleaes let
> > me know how to do it.
> >
> > <table>
> > <tbody>
> > <tr>
> > <td><b>gaurang</b></td>
> > </tr>
> > </tbody>
> > </table>
> >
> > I want "<b>gaurang</b>" text. I tried following way but, it shows empty
> > string
> >
> > JS.executeScript("arguments[0].innerHTMML",obj)
>
> --
> You received this message because you are subscribed to the Google Groups "webdriver" group.

David

unread,
Dec 16, 2012, 10:27:31 PM12/16/12
to webdriver
> And finally, the embedded table:
>
>   "//div/span[2]/div/table/tbody/tr/td[3]/table/tbody/tr[3]/td[2]"

> Not sure how CSS would handle embedded tables.

That's a good point, but at least you can define the CSS like this:

"div > span:nth-child(2) > div > table > tbody > tr > td:nth-child(3)
> table > tbody > tr:nth-child(3) > td:nth-child(2)"

If nth-child() doesn't work, give nth-of-type() a try. The CSS can be
tested in Firefix w/ tool like FirePath or Firefinder to see if it can
detect. If works, then it should likely work in Selenium too.

>> If you have multiple tables including tables within tables, this
is more complicated.

Under this circumstance, one other option that can work, if you know
relatively, what order/position the table of interest is, you can
specify it in XPath, w/o needing IDs or specifying the absolute (or
near absolute) XPath. This can be tested out in FirePath, Firefinder,
etc. to figure out what position the table is relative to the DOM.
Though this does assume the table position infrequently changes. If
it's based on custom web design, it might change often, but if the
table was generated with web libraries like dojo, etc., it's less
likely to deviate in changes.

example XPath string:

(//table/tbody/tr[3]/td[2])[3]

returns the 3rd node/element that matches the given XPath in
parenthesis. Replace 3 with number n to get nth element of given
XPath.

On Dec 15, 10:43 am, Bill Ross <r...@cgl.ucsf.EDU> wrote:
Reply all
Reply to author
Forward
0 new messages