how to get variable text from browser

J

Joe Pizzanley

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?
 
J

Jesús Gabriel y Galán

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

Joe Pizzanley

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

Jesús Gabriel y Galán

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

Jesús Gabriel y Galán

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top