2012/9/12 Charmaine Willemsen <lists / ruby-forum.com>

> 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