2012/9/12 Charmaine Willemsen <[email protected]>
> In this example i like to parse birthday and sexe
>
>
> <div class="floatleft">
> <label for="lblbirthday" id="lblbirthdayLabel">birthday</label>
> <span id="lblbirthday">6 apr 1961</span>
> </div>
> <div class="floatleft" style="margin-left: 50px;">
> <label for="lblsexe" id="lblsexeLabel">sexe</label>
> <span id="lblsexe">Male</span>
> </div>
>
> -----------------------------------------------------------------------------
>
> These ruby script will not work, please who can help me?
>
> options[:birthday] = find(:xpath, ".//span[contains(@id,
> \"lblbirthday\")]").text
> options[:sexe] = find(:xpath, ".//span[contains(@id,
> \"lblsexe\")]").text
>
> --
> Posted via http://www.ruby-forum.com/.
>
> 1. Nokogiri doesn't have method find, maybe he is using Capybara.
If so try to find with css since it's simpler:
options[:birthday] = find("span#lblbirthday").text
options[:sexe] = find("span#lblsexe").text
In any case Capybara converts css to xpath when searching.
2. You could do it with Nokogiri too. Here is complete example:
require 'nokogiri'
html = "<div class='floatleft'>
<label for='lblbirthday' id='lblbirthdayLabel'>birthday</label>
<span id='lblbirthday'>6 apr 1961</span>
</div>
<div class='floatleft' style='margin-left: 50px;'>
<label for='lblsexe' id='lblsexeLabel'>sexe</label>
<span id='lblsexe'>Male</span>
</div>"
doc = Nokogiri::HTML(html)
options = {}
options[:birthday] = doc.at_css('span#lblbirthday').content
options[:sexe] = doc.at_css('span#lblsexe').content
options.each { |key, value| puts "#{key}: #{value}" }
It outputs:
birthday: 6 apr 1961
sexe: Male
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en