Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #3881 > unrolled thread

how to get variable text from browser

Started byJoe Pizzanley <pizzazjoe@yahoo.com>
First post2011-05-03 12:14 -0500
Last post2011-05-04 02:03 -0500
Articles 6 — 2 participants

Back to article view | Back to comp.lang.ruby


Contents

  how to get variable text from browser Joe Pizzanley <pizzazjoe@yahoo.com> - 2011-05-03 12:14 -0500
    Re: how to get variable text from browser Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-03 12:24 -0500
      Re: how to get variable text from browser Joe Pizzanley <pizzazjoe@yahoo.com> - 2011-05-03 12:36 -0500
        Re: how to get variable text from browser Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-03 12:48 -0500
          Re: how to get variable text from browser Joe Pizzanley <pizzazjoe@yahoo.com> - 2011-05-03 12:58 -0500
            Re: how to get variable text from browser Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-05-04 02:03 -0500

#3881 — how to get variable text from browser

FromJoe Pizzanley <pizzazjoe@yahoo.com>
Date2011-05-03 12:14 -0500
Subjecthow to get variable text from browser
Message-ID<2c12d0a1cc624f5eefefe34ad6a18613@ruby-forum.com>
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/.

[toc] | [next] | [standalone]


#3882

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-05-03 12:24 -0500
Message-ID<BANLkTikFNg7TifhDu7bNeBJRf+tJ0j7ocA@mail.gmail.com>
In reply to#3881
On Tue, May 3, 2011 at 7:14 PM, Joe Pizzanley <pizzazjoe@yahoo.com> 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.

[toc] | [prev] | [next] | [standalone]


#3883

FromJoe Pizzanley <pizzazjoe@yahoo.com>
Date2011-05-03 12:36 -0500
Message-ID<8d6582f8bc7b97d12927a62f7c427fc9@ruby-forum.com>
In reply to#3882
> 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/.

[toc] | [prev] | [next] | [standalone]


#3884

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-05-03 12:48 -0500
Message-ID<BANLkTinZFmnToJf=YaXwn3DCvtg1P-dmgQ@mail.gmail.com>
In reply to#3883
On Tue, May 3, 2011 at 7:36 PM, Joe Pizzanley <pizzazjoe@yahoo.com> 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.  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.

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 = $ie.text.match /Files\s+\(\d+\)/
if m
  files_text = m[0]
else
  puts "Couldn't find the text in the page"
end

Jesus.

[toc] | [prev] | [next] | [standalone]


#3885

FromJoe Pizzanley <pizzazjoe@yahoo.com>
Date2011-05-03 12:58 -0500
Message-ID<7fc3286fa1975c7621704ba2a9b189a4@ruby-forum.com>
In reply to#3884
> m = $ie.text.match /Files\s+\(\d+\)/

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

Thanks Jesús!

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3908

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-05-04 02:03 -0500
Message-ID<BANLkTik-s90ZZNTShpHhzB7RwEr6yoOHSA@mail.gmail.com>
In reply to#3885
On Tue, May 3, 2011 at 7:58 PM, Joe Pizzanley <pizzazjoe@yahoo.com> wrote:
>> m = $ie.text.match /Files\s+\(\d+\)/
>
> That's what I needed - the magic of match.
>
> Thanks Jesús!

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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web