how to get variable text from browser

Discussion in 'Ruby' started by Joe Pizzanley, May 3, 2011.

  1. The browser page I'm working with has a semi-variable text group: "Files
    (12)"

    I want to turn that into a variable in my Ruby script: myfiles = "Files
    (12)"

    I surprised myself and figured out the regex to account for the changing
    count: Files \(\d{0,999}\) but how do I grab it off of the browser
    page?

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Pizzanley, May 3, 2011
    #1
    1. Advertisements

  2. On Tue, May 3, 2011 at 7:14 PM, Joe Pizzanley <> wrote:
    > The browser page I'm working with has a semi-variable text group: "Files
    > (12)"
    >
    > I want to turn that into a variable in my Ruby script: myfiles = "Files
    > (12)"
    >
    > I surprised myself and figured out the regex to account for the changing
    > count: Files \(\d{0,999}\) but how do I grab it off of the browser
    > page?


    So, you want to fetch the page calling the URL, locate that text and
    extract the number part between parens?
    If so, I would use open-uri to read the page, then use Nokogiri to
    parse it, come up with an XPath or css selector expression that takes
    you to the text node, get the contents of the node, and then apply
    your regular expression to that. Something like:

    require 'open-uri'
    require 'nokogiri' # 'gem install nokogiri' first

    doc = Nokogiri::HTML(open(your_url).read)
    element = doc.css("your css selector expression here")
    # or element = doc.xpath("your xpath expression here") if you find it
    more suitable
    m = element.text.match /Files \((\d+)\)/
    puts m[1] if m

    By the way, \d{0,999} doesn't mean a number between 0 and 999, it
    means a digit repeated between 0 and 999 times, so for example this
    would match "Files ()", as well as "Files (23423283423423423423423)".
    You might to make it a little bit more flexible with the whitespace
    between the word Files and the parens, something like
    /Files\s+\((\d+)\)/

    Hope this helps,

    Jesus.
     
    Jesús Gabriel y Galán, May 3, 2011
    #2
    1. Advertisements

  3. > So, you want to fetch the page calling the URL, locate that text and
    > extract the number part between parens?


    Not necessarily. I want to end up with "Files (7)" or whatever number it
    happens to be as my variable value. Then I'm going to run a check on
    it.



    > Something like:
    >
    > require 'open-uri'
    > require 'nokogiri' # 'gem install nokogiri' first
    >
    > doc = Nokogiri::HTML(open(your_url).read)
    > element = doc.css("your css selector expression here")
    > # or element = doc.xpath("your xpath expression here") if you find it
    > more suitable
    > m = element.text.match /Files \((\d+)\)/
    > puts m[1] if m


    I'm a beginner and this is way too complicated for me to handle.
    Isn't there a simpler way? $ie.text will grab ALL the text on the
    browser page, but I just want that small bit.

    --
    Posted via http://www.ruby-forum.com/.
     
    Joe Pizzanley, May 3, 2011
    #3
  4. On Tue, May 3, 2011 at 7:36 PM, Joe Pizzanley <> wrote:
    >> So, you want to fetch the page calling the URL, locate that text and
    >> extract the number part between parens?

    >
    > Not necessarily. I want to end up with "Files (7)" or whatever number it
    > happens to be as my variable value. =A0Then I'm going to run a check on
    > it.
    >
    >
    >
    >> Something like:
    >>
    >> require 'open-uri'
    >> require 'nokogiri' # 'gem install nokogiri' first
    >>
    >> doc =3D Nokogiri::HTML(open(your_url).read)
    >> element =3D doc.css("your css selector expression here")
    >> # or element =3D doc.xpath("your xpath expression here") if you find it
    >> more suitable
    >> m =3D element.text.match /Files \((\d+)\)/
    >> puts m[1] if m

    >
    > I'm a beginner and this is way too complicated for me to handle.
    > Isn't there a simpler way? $ie.text will grab ALL the text on the
    > browser page, but I just want that small bit.


    OK, you need to give a little bit more context. With the description
    you gave we couldn't know if you had already something or not. So, let
    me guess, $ie is a variable that holds a Watir browser? If so, there
    are two possibilities which might work, from your description. You
    might first locate the node in the DOM which contains the text, or you
    could try to run your regexp against the full text of the page. I
    guess $ie.text will return everything that is not inside an HTML tag,
    so it might work, depending on the rest of the page:

    m =3D $ie.text.match /Files\s+\(\d+\)/
    if m
    files_text =3D m[0]
    else
    puts "Couldn't find the text in the page"
    end

    Jesus.
     
    Jesús Gabriel y Galán, May 3, 2011
    #4
  5. > m =3D $ie.text.match /Files\s+\(\d+\)/

    That's what I needed - the magic of match.

    Thanks Jes=C3=BAs!

    -- =

    Posted via http://www.ruby-forum.com/.=
     
    Joe Pizzanley, May 3, 2011
    #5
  6. On Tue, May 3, 2011 at 7:58 PM, Joe Pizzanley <> wrote:
    >> m =3D $ie.text.match /Files\s+\(\d+\)/

    >
    > That's what I needed - the magic of match.
    >
    > Thanks Jes=FAs!


    I'm glad you got your answer. I think it would be good anyway to read
    a little bit about XPath and how you can use it with Watir, because
    you might find a situation where matching against all the page text is
    not suitable, and you need to find specific elements in the HTML:

    http://wiki.openqa.org/display/WTR/XPath

    Jesus.
     
    Jesús Gabriel y Galán, May 4, 2011
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mfglinux
    Replies:
    11
    Views:
    786
    Roberto Bonvallet
    Sep 12, 2007
  2. Nathan Sokalski
    Replies:
    4
    Views:
    7,271
    Nathan Sokalski
    Jul 8, 2008
  3. sandhu
    Replies:
    1
    Views:
    838
    Alvin Bruney [MVP]
    May 17, 2007
  4. David Filmer
    Replies:
    19
    Views:
    294
    Kevin Collins
    May 21, 2004
  5. Replies:
    3
    Views:
    188
    Martin Honnen
    Jun 29, 2007
Loading...

Share This Page