Login web page using mechanize

new to ruby, love the language. read programmatic programmers guide to
get started and i understand the basics of ruby language. im just
starting to learn gems, ive played with mysql, crypt, and now mechanize.
I have yet to write a successful program with any of the gems but im
hooked on mechanize. ok so here is what im trying to do…
i am a student in itt tech online course. to get credit for attendance i
have to send emails everyday through the webpage. so i thought “yesss!!!
i can write a program using mechanize to do this for me.”
First question: is that even possible?

next i can get to the webpage and can print the forms but dont know how
to fill it out to submit it. here is my code and what i see when
executed:

#THIS IS MY CODE
require ‘rubygems’
require ‘mechanize’

agent = Mechanize.new
page = agent.get(‘http://www.distance-education.itt-tech.edu/’)

form = page.forms.each do |form|
pp form
end

#THIS IS WHAT IS PRINTED OUT
#<Mechanize::Form
{name “loginForm”}
{method “POST”}
{action “javascript:exit(’/online/valdoc/cliksIndep_prcsslogin’)”}
{fields
[hidden:0x1a09478 type: hidden name: fbcorporateid value: 2222]
[hidden:0x1a093a0 type: hidden name: fbautologin value: 0]
[hidden:0x1a092d4 type: hidden name: fbloginhome value: ITT]
[hidden:0x1a09208 type: hidden name: fbGrade value: null]
[hidden:0x1a0913c type: hidden name: afbTestID value: null]
[hidden:0x1a09070 type: hidden name: fbPrgLnchId value: null]
[hidden:0x1a08fa4 type: hidden name: fbuid value: null]}
{radiobuttons}
{checkboxes}
{file_uploads}
{buttons [imagebutton:0x1a08ef0 type: image name: Login value: ]}>

-ok so i see the form, but i dont know which one i would put my user
name and password into.
-Also i use firebug to see the names of the text-fields on the actual
web page and their “names” are not in the list of fields that are
printed out.

-so my next question is how do i fill out the username and password
fields and click the submit button to get to the next page? id like to
figure it out by myself but i have not been having much luck. any help
is appreciated thank you!

Your problem is that username and password fields are not inside form.
Open Firebug and inspect those fields and you will see that.
Instead don’t vorry where it is, just select field with name fblogincd
and fbpassword,
fill in with data, and click on input element with id sImage1. That’s
all.

Is your requirement strictly Mechanize? Mechanize is good for simpler
sites
without javascript. If you need javascript (you will need it, before or
later) then Capybara is better solution. I could write you simple script
in
Capybara to do this task.

2012/11/4 john smith [email protected]

thank you. i am going to try your suggestion. is copybara a gem? i will
look into that as well. i would appreciate a simple script but i would
like to see if i can figure it out myself first. i knew that the
username and password fields were not in the form but i didnt think i
could just fill it out if it wasnt there. i will try it and get back to
you. thanks again.

Yes, Capybara is a gem. Google for: ruby capybara.
Ok, if you need help call me.

2012/11/5 john smith [email protected]

It’s ok, but I prefer google groups or better stackoverflow.com, because
this could be interesting to other people. But of course if you need
some
help you can contact me by email.

2012/11/5 john smith [email protected]

“Иван Бишевац” [email protected] wrote in post #1082870:

Yes, Capybara is a gem. Google for: ruby capybara.
Ok, if you need help call me.

2012/11/5 john smith [email protected]

Do you mind if i just e-mail you if i need further help?

the “action” in the form = {action
“javascript:exit(’/online/valdoc/cliksIndep_prcsslogin’)”}

-which im assuming means i need javascript, which mechanize cant handle?

dave i tried your script,one i came up with was similar only without the
rescue, and it raises the rescue “Not on login page or expected fields
not found.”

so im guessing i need to use copybara, which i will research and post
after i figure it out. thank you guys for all the help so far

By the way, it’s not copybara but capybara :slight_smile:

2012/11/5 john smith [email protected]

“Иван Бишевац” [email protected] wrote in post #1082996:

By the way, it’s not copybara but capybara :slight_smile:

2012/11/5 john smith [email protected]

haha google auto corrected it for me. thank you though!

ok i need help with capybara. i keep finding pages with info about using
it with cucumber and sinatra and im not sure what any of that is. do i
need those for what im trying to do?

On 4 Nov 2012, at 19:41, john smith [email protected] wrote:

next i can get to the webpage and can print the forms but dont know how
form = page.forms.each do |form|
pp form
end

You could try this. I have not tested it as my mechanize is broken at
present.

agent = Mechanize.new
page = agent.get(‘http://www.distance-education.itt-tech.edu/’)
form = page.forms.first
begin
form.fblogincd = user
form. fbpassword = password
form.submit
rescue
pp page
raise “Not on login page or expected fields not found.”
end

Dave.

Start with http://stackoverflow.com/a/4172101/507018 and look at
documentation at https://github.com/jnicklas/capybara.

2012/11/5 john smith [email protected]

looks like i have a lot of reading to do awesome. might take me a day or
so but ill post back with any more questions or progress.

I’ve always found Watir-Webdriver to be a very powerful tool if you want
complex browser automation. It’s particularly good with JavaScript as
well as you can write and execute your own JavaScript commands in the
page.

i followed the outline from the github capybara page and here is what i
came up with.

require ‘capybara’

#Capybara.default_driver = :selenium

page = visit(‘http://www.distance-education.itt-tech.edu/’)

page.fill_in(‘fblogincd’, :with => ‘username’)
page.fill_in(‘fbpassword’, :with => ‘password’)

page2 = page.click_button(‘Login’)

pp page2

This is what i get:

undefined method `visit’ for Capybara:Module (NoMethodError)

at first i thought it meant that i didnt have it set to a variable
because i first tried

visit(‘http://www.distance-education.itt-tech.edu/’)

but when i got an error i figured it was because i didnt set it to a
variable.
right now im not sure but this is what i came up with so far. any input
would be appreciated thank you

“Иван Бишевац” [email protected] wrote in post #1083041:

Start with http://stackoverflow.com/a/4172101/507018 and look at
documentation at https://github.com/jnicklas/capybara.

2012/11/5 john smith [email protected]

wow I’ve heard of git hub, even have an account, never used it but that
capybara page is full of information

No, you have to set driver first. Also you have to set run_server to
false.
Did you get link on stackoverflow I gave you?

2012/11/8 john smith [email protected]

oh man that is cool!!! i can actually see it bring up the page and fill
in the fields!!! im going to keep going with this and let you know how i
make out.

that example on stack overflow works thank you!

oh and i know

#Capybara.default_driver = :selenium

is a comment. i wanted to get to the page first before messing with the
driver.

Just keep in mind, for efficiently parsing data from web you have to
learn
css and/or xpath selectors. That’s the key.

2012/11/8 john smith [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs