tags:

views:

103

answers:

5

I am using Watir to write some tests for a web application. I need to get the text 'Bishop' from the HTML below but can't figure out how to do it.

<div id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view" style="display: block;">
   <div class="workprolabel wpFieldLabel">
    <span title="Please select a courtesy title from the list.">Title</span>&nbsp;<span class="validationIndicator wpValidationText"></span>
   </div>
   <span class="wpFieldViewContent" id="dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value"><p class="wpFieldValue ">Bishop</p></span>
  </div>

Firebug tells me the xpath is:

html/body/form/div[5]/div[6]/div[2]/div[2]/div/div/span/span/div[2]/div[4]/div[1]/span[1]/div[2]/span/p/text()

but I cant format the element_by_xpath to pick it up.

A: 

Try

//span[@id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']//text()

EDIT:

Maybe this will work

path = "//span[@id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']/p";
ie.element_by_xpath(path).text

And check if the span's id is constant

pablochan
thanks for the answer, i've tried it but it doesnt seem to return the text:bleh = @@ie.element_by_xpath("//span[@id='dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5b45385e5f45b_view_value']/p/text()") puts blehthis just returns whitespace
ryanthescot
tried it with setting the path but got this error:NoMethodError: undefined method `text' for nil:NilClass.it just seems to not want to pick up the element. there must surely be a way to do it
ryanthescot
+1  A: 

You should be able to access the paragraph right away if it's unique:

my_p = browser.p(:class, "wpFieldValue ")
my_text = my_p.text

See HTML Elements Supported by Watir

katmoon
sorry forgot to mention it's not unique
ryanthescot
Try to access the paragraph first using xpath.Then use .html method to see what's inside the paragraph. Maybe you navigate to a wrong paragraph.
katmoon
not sure what you mean here. when i tried the code above i just got unable to locate element by class "wpFieldValue".
ryanthescot
Try without xpath as shown below and see if you still cannot access the paragraph: my_span = browser.span(:id, "dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value") my_p = my_span.p(:class, "wpFieldValue ") puts my_p.text
katmoon
genius - that worked. also it needed the space off the end like you said. thanks very much.
ryanthescot
A: 

Maybe you have an extra space in the end of the name?

<p class="wpFieldValue ">
katmoon
the space is there in the html so i dont think that should matter?
ryanthescot
taking the space off worked like you suggested. this seems a bit strange to me since the space is in the html, but thanks for your help
ryanthescot
A: 

Try one of these (worked for me, please notice trailing space after wpFieldValue in the first example):

browser.p(:class => "wpFieldValue ").text
#=> "Bishop"

browser.span(:id => "dnn_ctr353_Main_ctl00_ctl00_ctl00_ctl07_Field_048b9dfa-bc64-42e4-8bd5-b45385e5f45b_view_value").text
#=> "Bishop"
Željko Filipin
thanks - i managed to get it working by doing what katmoon suggested. weirdly i had to take the trailing space out for it to work
ryanthescot
A: 
Jazzezravi