-2

I have the following code:

<tr>
<td Width="50%" align="left">
<span id="ctl00_lblTotalDesc">Ext. Subtotal</span></td>
<td Width="50%" align="right">
<span id="ctl00_lblTotalValue">100,087,000.00</span></td>
</tr>

I used the following to grab the value of the 2nd span element:

spanValue = $('#ctl00_lblTotalValue').text();

But this doesn't seem to work in Spock/Geb. I get the following error:

TypeError: $(...).text is not a function

What am I doing wrong

I get the following error if I use, $('#ctl00_lblTotalValue')

[object HTMLTableElement]

Firefox console was not useful so used Chrome console.

In Chrome: if I try $('#ctl00_lblTotalValue'), I get

<span id="ctl00_lblTotalValue">100,087,000.00</span>

But .text() - gives Type error: Object # has no method 'text'

vijay pujar
  • 1,683
  • 4
  • 19
  • 32
  • 1
    looks like `$` is not referring to jQuery here try `jQuery('#ctl00_lblTotalValue').text()` – Arun P Johny Dec 04 '13 at 15:26
  • 1
    what are the other libraries used in the page? also have you properly included jQuery in the page – Arun P Johny Dec 04 '13 at 15:27
  • 2
    check `alert($ === jQuery)` – A. Wolff Dec 04 '13 at 15:27
  • When I try to print the value it returns null. Hi Arun, I have used plenty of selectors on that page and all are working fine with $ function – vijay pujar Dec 04 '13 at 15:37
  • You seem to be mixing javascript with Groovy, and Geb with jQuery and not for the first time. These things are not the same. Also, `TypeError: $(...).text is not a function` is not a Geb/Spock/Groovy error, it's a javascript error. What are you actually asking about? – erdi Dec 05 '13 at 12:41
  • Erdi, did you down vote me? I have done a lot of research and was making a point if it is a limitation with Geb. Can you explain me why did you downvote – vijay pujar Dec 05 '13 at 14:05
  • Can you please explain if I can grab the value of the span element in GEB – vijay pujar Dec 05 '13 at 14:07

3 Answers3

1

It seems you have another js framework overriding the $ function. it means you have to use jQuery('...') instead. The other point you have to consider is, as far as I know in all js frameworks I've worked with, the $ function does the same thing, but sometimes it selects the first matched html element then when your output is like:

[object HTMLTableElement]

it means the html object is not a span element. try:

$('#ctl00_lblTotalValue').length

if the result was greater than 1, it mean you have more than one html element with this same id.

Mehran Hatami
  • 12,723
  • 6
  • 28
  • 35
  • By the way, I used the console on Firefox and the results are very inconsistent. I tried using chrome and if I try $('#ctl00_lblTotalValue'), I get 100,087,000.00. But .text() - gives Type error: Object # has no method 'text' – vijay pujar Dec 04 '13 at 16:18
  • don't worry there are still a lot of ways to do this. just to make sure if your `$` function is working, try `$(/*let it be empty*/).jquery` if the result is your jQuery version it means the $ function is the original one, if not you'd better try `jQuery('#ctl00_lblTotalValue')` instead of `$('#ctl00_lblTotalValue')`. – Mehran Hatami Dec 04 '13 at 17:20
  • but if your `$` function is working and you still have this problem, it means, besides jQuery, you have another JavaScript library in your page and this library has removed some jQuery functions in your jQuery framework, you can do this like this: `jQuery.fn.text = null; delete jQuery.fn.text;`, if it was the problem you can try `$('#ctl00_lblTotalValue').html()`, in your case it will result the same as `$(...).text()`, let me know if it won't work. – Mehran Hatami Dec 04 '13 at 17:20
  • looks like there is a limitation with Geb. Sorry about the confusion but the selectors in my code are all based on Geb not the complete jQuery. However it does support most of the jQuery selectors but looks like there is a limitation. – vijay pujar Dec 04 '13 at 23:26
0

Simply try this:

$("span", id:"ctl00_lblTotalValuee").text()

Assuming that the value ct100 is a constant and is not generated by your server side application randomly.

grepit
  • 21,260
  • 6
  • 105
  • 81
-1

Is "ctl00_lblTotalValue" the id you assigned to the span or is it a server-side control getting its ID auto-assigned by the environment?

For example, if I have this code in my .NET aspx page

<div id="pnlHeader" runat="server"></div>

it gets rendered in the html page as

<div id="ctl00_pnlHeader"></div>

If that is the case, I need to grab it in the jquery using this syntax

$("#<%= pnlHeader.ClientID%>").text()

If yours is a server-side control, you may need to grab the text using

spanValue = $("#<%= lblTotalValue.ClientID%>").text();
Binita Mehta
  • 339
  • 1
  • 8
  • This is the selector I defined in the Spock page. I grabed the selector from the web page. The ID generated for the span element is a lot more complicated that I have written above. The actual ID is: ctl00_cphMain_QuoSummary_gridTotals_ctl02_lblTotalValue – vijay pujar Dec 04 '13 at 15:57
  • even though ID looks like it is generated at the server side, it is always the same on the page. Can you please write the jQuery here. – vijay pujar Dec 04 '13 at 17:12
  • Can you clarify what you mean by "write the jQuery here"? I already had it in the answer as **spanValue = $("#<%= lblTotalValue.ClientID%>").text();** – Binita Mehta Dec 04 '13 at 22:15