How to getText() of a element through JS

6,283 views
Skip to first unread message

sai kiran

unread,
May 2, 2012, 8:41:11 PM5/2/12
to webdriver
Hi,

can some one help me to get text() of a cell in a table through JS. Also, i have tried gettext() of 2.0 to get the value but all the ways it shows null value. No idea, where it is going wrong.trying luck with JS.

Thanks
Sai

Abhijeet Bedagkar

unread,
May 3, 2012, 12:15:11 AM5/3/12
to webd...@googlegroups.com
Please try following code, working for me.


strin cellText = (string)_RemoteWebDriver.ExecuteScript("return document.getElementById('htmlid').rows[1].cells[0].innerText;").ToString().Trim();


Sai

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
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.

darrell

unread,
May 3, 2012, 8:38:50 AM5/3/12
to webdriver
There isn't really enough information here to help you with why
getText() is not working for you. Abhijeet's example assumes the table
has an id attribute. It is also hardcoded to row 2, column 1. If it
does not, his example will not work. The problem with generating your
own Javascript call is that you first have to figure out how to find
the cell.

Personally, rather than re-inventing the getText() method, I would
figure out why the getText() method is not working for you.

First step is confirming you have found the cell you want the text
for. Once you are sure you have the WebElement reference for the cell
(TD) you can try to figure out why getText() isn't working for you.
You might find you aren't getting the correct reference and there is
nothing wrong with getText().

Usually the simplest way for us to help you is to give us enough
information that we could write the automation code. If we know what
the answer is, it is easy to tell you what is going wrong.

Darrell

sai kiran

unread,
May 3, 2012, 10:31:27 AM5/3/12
to webd...@googlegroups.com
Hi Darrell,

Please find the HTML code

<tr bgcolor="#FFFFFF">

    <td id="SysIdFromIndex0" class="c"><input type="hidden" value="7" name="fSysId">
    330</td>

    <td class="txt"> Licence Flight Fleet</td>

    <td class="txt">332HF</td>

    <td class="c">
   
    <a href="javascript:openPopup('0')">Edit</a>
   
    <a id="0" href="javascript:deleteDetails('0')">Delete</a>

  </td>

</tr>


driver.findElement(By.xpath(//tr/td[1]).getText();

driver.findElement(By.id("SysIdFromIndex0")).getText();

Tried with absolute xpath but every time it returns null value and doesn't fail...

Thanks








Mark Collin

unread,
May 3, 2012, 11:04:55 AM5/3/12
to webd...@googlegroups.com

That XPath will find the first <td> child of the first <tr> element on the page.  I wouldn’t expect it to return anything valid to be honest, especially knowing the propensity for some devs to hack tables in for layout rather than doing it properly…

Anil Shahi

unread,
May 10, 2012, 12:01:29 PM5/10/12
to webd...@googlegroups.com
Hi ,


Try cssSelector it might help


  driver.findElementBy.cssSelector(" tr>td[id='your td Id']").getText();
 
 

   Anil

Mark Collin

unread,
May 10, 2012, 3:30:56 PM5/10/12
to webd...@googlegroups.com

If you know an ID don’t even bother with all the extra faff, just do a:

 

driver.findElement(By.id(“<ID_Here>”));

 

The above should be your default locator strategy.

darrell

unread,
May 15, 2012, 9:14:59 AM5/15/12
to webdriver
If the id for the cell you are seeking is really "*SysIdFromIndex0*"
then reject the application because the id is not valid. From the HTML
standard:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may
be followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").

If the HTML code does not follow the HTML standard then automation
tools will be unpredictable.

On May 3, 10:31 am, sai kiran <skrn1...@gmail.com> wrote:
> Hi Darrell,
>
> Please find the HTML code
>
> <tr bgcolor="#FFFFFF">
>
>     <td id="*SysIdFromIndex0*" class="c"><input type="hidden" value="7"
> name="fSysId">
>     *330*</td>
>
>     <td class="txt"> Licence Flight Fleet</td>
>
>     <td class="txt">332HF</td>
>
>     <td class="c">
>
>     <a href="javascript:openPopup('0')">Edit</a>
>
>     <a id="0" href="javascript:deleteDetails('0')">Delete</a>
>
>   </td>
>
> </tr>
>
> *driver.findElement(By.xpath(//tr/td[1]).getText();*
>
> *driver.findElement(By.id("**SysIdFromIndex0**")).getText();*

sai kiran

unread,
May 15, 2012, 12:51:53 PM5/15/12
to webd...@googlegroups.com
Hi Darell,

Thanks for the info. I have resolved the issue  with JS

document.getElementById("SysIdFromIndex0").innerText; 

But, pl let me know for any alternative workaround using selenium.Because, i have couple of issues with the same problem.

Thanks

Mark Collin

unread,
Aug 21, 2012, 5:38:47 PM8/21/12
to webd...@googlegroups.com
The solution is really mimic what a real user would do.

What would a real user do to make the hidden element visible?

-----Original Message-----
From: webd...@googlegroups.com [mailto:webd...@googlegroups.com] On
Behalf Of mary walshe
Sent: 20 August 2012 17:14
To: webd...@googlegroups.com
Subject: Re: [webdriver] Re: How to getText() of a element through JS

I have been looking into this today and have found that webdriver will
return null when attempting to getText() of a hidden element.

This is due to the fact that webdriver tries to emulate the user experience
and a user cannot interact with hidden fields.

I think javascript is the only solution to this:
http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions#Q:_Why_is_it
_not_possible_to_interact_with_hidden_elements?

--
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/-/1gD7ZaQBSe4J.
Reply all
Reply to author
Forward
0 new messages