Forum: Ruby Not able get the label text incurred with <input> element

Posted by Love U Ruby (my-ruby)
on 2013-02-05 23:44
I do have a below `HTML`:

    <div id="filtersetedit_fieldNames" title="Please first select a list
to filter!">

    <input value="5418630" name="NameID" type="checkbox"> John
    <input value="6360899" name="NameID" type="checkbox"> Ram
    <input value="9556609" name="NameID" type="checkbox"> Smith
    <input value="20156687" name="NameID" type="checkbox"> Paul

    </div>

Now I have to check the check-box using selenium-webdriver. But I tried
to get extract the values as `John,Ram etc`.But my code didn't worked at
all.

***`CODE`***

    driver.find_elements(:xpath,"//div[contains(@id,'filtersetedit_fieldNames')]/input")
do |x|

        puts x.text.strip

    end

But none of the values are printed. Any idea how to get it done.
Posted by Joel Pearson (virtuoso)
on 2013-02-06 00:59
The text is in the div, not linked to the checkboxes. Looks like poorly 
organised html.

If you know the value you want, then you could do this (examples given 
in watir-webdriver, which you REALLY need to start using for the sake of 
your own sanity):

driver.checkbox(:value => '9556609').set

If you have to work out which of the div's text rows goes with which 
checkbox you'd probably have to do it via indices. Extracting the div's 
text didn't take the newlines so I had to split by spaces.

my_div = driver.div(:id => 'filtersetedit_fieldNames')
name_array = my_div.text.split
name_array.each_with_index do |name, idx|
  puts "name: #{name} value: #{my_div.checkbox(:index, idx).value}"
end

output:

name: John value: 5418630
name: Ram value: 6360899
name: Smith value: 9556609
name: Paul value: 20156687

Once you have the names and corresponding values you can set whichever 
one you like.

And slap the person who wrote that html.
Posted by Love U Ruby (my-ruby)
on 2013-02-06 07:29
Joel Pearson wrote in post #1095429:
> The text is in the div, not linked to the checkboxes. Looks like poorly
> organised html.
>
> If you know the value you want, then you could do this (examples given
> in watir-webdriver, which you REALLY need to start using for the sake of
> your own sanity):
>
> driver.checkbox(:value => '9556609').set
>
> If you have to work out which of the div's text rows goes with which
> checkbox you'd probably have to do it via indices. Extracting the div's
> text didn't take the newlines so I had to split by spaces.
>
> my_div = driver.div(:id => 'filtersetedit_fieldNames')

I am using selenium-web driver at all. But your above line is with 
selenium one or watir web-driver at all? tell me please.

Thanks
Posted by Joel Pearson (virtuoso)
on 2013-02-06 08:32
Love U Ruby wrote in post #1095452:
> Joel Pearson wrote in post #1095429:
>> examples given in watir-webdriver
> your above line is with
> selenium one or watir web-driver at all? tell me please.
Posted by Love U Ruby (my-ruby)
on 2013-02-06 08:41
Joel Pearson wrote in post #1095461:
> Love U Ruby wrote in post #1095452:
>> Joel Pearson wrote in post #1095429:
>>> examples given in watir-webdriver
>> your above line is with
>> selenium one or watir web-driver at all? tell me please.


Could you just tell me how and what "name_array" contains its elements? 
If I get that,I would convert it into selenium one.


Thanks,
Posted by Hans Mackowiak (hanmac)
on 2013-02-06 08:50
why not try it yourself? or read what others say:

"Extracting the div's
text didn't take the newlines so I had to split by spaces."
Posted by Love U Ruby (my-ruby)
on 2013-02-06 09:08
Hans Mackowiak wrote in post #1095463:
> why not try it yourself? or read what others say:
>
> "Extracting the div's
> text didn't take the newlines so I had to split by spaces."

I don't have watir-web driver installed in my PC. Now so I am trying to 
understand it by logic.

name_array= [John,Ram,Smith,Paul] If it really this,then I think my one 
should also work for me:

driver.find_elements(:xpath,"//div[contains(@id,'filtersetedit_fieldNames')]/input").each 
do |x|

        puts x.text.strip

end
Posted by Joel Pearson (virtuoso)
on 2013-02-06 09:53
Love U Ruby wrote in post #1095465:
> I don't have watir-web driver installed in my PC. Now so I am trying to
> understand it by logic.
>
> name_array= [John,Ram,Smith,Paul] If it really this,then I think my one
> should also work for me:
>
>
driver.find_elements(:xpath,"//div[contains(@id,'filtersetedit_fieldNames')]/input").each
> do |x|
>
>         puts x.text.strip
>
> end

You've completely missed my point.

1) The DIV, not the Checkbox, contains the Name you want; so each
"x.text" will be blank.

2) Watir-Webdriver contains Selenium-Webdriver. Switching to it is
simple and it was specifically made to make writing browser-automating
code easier for you. I see no reason to persist in using the more
difficult selenium API when there's a layer already designed to help
Rubyists use it more effectively. Use the open-source tools that
selfless people have expended great effort to make.
See this (especially the illustrative image):
http://watirmelon.com/2010/04/10/watir-selenium-webdriver/

3) Do your own research first! There are several ways to do what you
want here, and when you completely fail to take in the information
provided, and ask questions which have already been answered, you will
find less and less help being offered to you.
Posted by Love U Ruby (my-ruby)
on 2013-02-06 13:53
Joel Pearson wrote in post #1095466:
> Love U Ruby wrote in post #1095465:
>> I don't have watir-web driver installed in my PC. Now so I am trying to
>> understand it by logic.
>>
>> name_array= [John,Ram,Smith,Paul] If it really this,then I think my one
>> should also work for me:

Now I tried the code as below against the HTML below:


<div id="filtersetedit_fieldNames" title="Please first select a list to
filter!"> <input value="5418630" name="NameID" type="checkbox"> Joh 
Das<input value="6360899" name="NameID" type="checkbox"> Ram Roy <input
value="9556609" name="NameID" type="checkbox"> Smith<input
value="20156687" name="NameID" type="checkbox"> Paul </div>


Code:

driver.find_elements(:id,"filtersetedit_fieldNames").each do |x|
puts x.text
#puts index
break if x.text == "LocationAttributes:Currency Type"
index = index + 1
end

driver.find_elements(:name, "candidateFieldIds")[index].click


But it prints some thing like below :

"John Das Ram Roy Smith Paul"

From where I am not able to find the the "John Das" index number to
check the check box associated with it.
Posted by Joel Pearson (virtuoso)
on 2013-02-06 14:39
This:
driver.find_elements(:id,"filtersetedit_fieldNames").each do |x|

Will give you only 1 result. That's not a loop. Read my example more 
carefully.

If you're getting spaces within individual names, you'll need to extract 
the html from the Div and parse that using Regex to find the appropriate 
checkbox name.

Here is an example of the Regex required to find the checkbox value for 
"Joh Das"
http://www.rubular.com/r/yTrqoCRo5v
Posted by Love U Ruby (my-ruby)
on 2013-02-06 15:06
Joel Pearson wrote in post #1095497:
> This:
> driver.find_elements(:id,"filtersetedit_fieldNames").each do |x|
>
> Will give you only 1 result. That's not a loop. Read my example more
> carefully.


I know "driver.page_source" to get the full html of a page,but no idea 
hot to get such element source. As you proposed. :(
Posted by Joel Pearson (virtuoso)
on 2013-02-06 15:42
I think this should do it:
driver.find_element(:id,"filtersetedit_fieldNames").attribute('innerHTML')
Posted by Love U Ruby (my-ruby)
on 2013-02-06 15:53
Joel Pearson wrote in post #1095530:
> I think this should do it:
> driver.find_element(:id,"filtersetedit_fieldNames").attribute('innerHTML')

Yes,the above worked.

Now, one point is that - the values say "Smith" will be in my Excel 
column,from there how should I create such "RegEx". I am thinking about 
that.
Posted by Joel Pearson (virtuoso)
on 2013-02-06 16:05
Regex supports #{variable}.

irb(main):016:0> name = "Ram Roy"
=> "Ram Roy"
irb(main):017:0> a.match(/value="(\d+)".+#{name}/)[1]
=> "6360899"
Posted by Ryan Victory (Guest)
on 2013-02-06 16:10
(Received via mailing list)
Google. Google. Google. Seriously, how difficult is it to type in
"Regex" into Google? I really feel like you are way over your head with
what you are trying to do, and you're using this forum/mailing list as
your personal team of programmers. I'm not normally one to speak out
like this, but I have only been here for about a month and have watched
you spam the list with constant questions about language and programming
fundamentals in general. Pick up a good book on Ruby. Pick up a good
book on programming. No, I won't recommend any (Google). If, after
reading and trying your best to figure stuff out on your own, you still
have questions, then by all means ask the list.

Joel, I commend you for your constant help with this person, but you're
basically being used as a free programmer.

-Ryan
Posted by Love U Ruby (my-ruby)
on 2013-02-06 16:26
Ryan Victory wrote in post #1095540:
> Google. Google. Google. Seriously, how difficult is it to type in
> "Regex" into Google? I really feel like you are way over your head with
> what you are trying to do, and you're using this forum/mailing list as


I don't know why you made such "harsh" comment. Is this forum not for a 
newbie? And If you think that I am using this to drive my team, really 
you are - not to say....
Posted by Joel Pearson (virtuoso)
on 2013-02-06 16:43
Ryan Victory wrote in post #1095540:
> Joel, I commend you for your constant help with this person, but you're
> basically being used as a free programmer.

Thanks :)
The best way to learn is to teach. I'm very new at this myself, so I'm 
basically building up my own skillset by doing this kind of thing.
I agree with the overall sentiment as well: You shouldn't ask other 
people until exhausting your own avenues of investigation.
Posted by Love U Ruby (my-ruby)
on 2013-02-06 18:54
Joel Pearson wrote in post #1095549:
> Ryan Victory wrote in post #1095540:
>> Joel, I commend you for your constant help with this person, but you're
>> basically being used as a free programmer.
>
> Thanks :)
> The best way to learn is to teach. I'm very new at this myself, so I'm
> basically building up my own skillset by doing this kind of thing.
> I agree with the overall sentiment as well: You shouldn't ask other
> people until exhausting your own avenues of investigation.

Thank you very much :)@joel. Please don't think I am using you for my 
sake,but I am taking help from you to gap of my knowledge.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.