How to get variable text from browser

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/.

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.

···

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?

Can you arrange for the text group you want passed to your program to be
passed via the URI used to access your program? For instance, if someone
clicks a link in the page that contains "Files (12)" that points to this:

    http://example.com/cgi/your_script.rb

. . . can you arrange for it to send this instead?

    http://example.com/cgi/your_script.rb?myfiles=Files (12)

If so, you can use the Ruby/CGI library to parse that:

    require 'cgi'
    cgi = CGI.new

    myfiles = cgi['myfiles']

(code untested, dredged from memory; hopefully it would work as written)

···

On Wed, May 04, 2011 at 02:14:13AM +0900, 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?

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]

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/\.

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.

···

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.

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/\.

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.

···

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!