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!

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

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 <lists@ruby-forum.com>

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/&#39;\)

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!

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

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.

···

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

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/&#39;\)

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/&#39;\)
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.

···

On 4 Nov 2012, at 19:41, john smith <lists@ruby-forum.com> wrote:

=================================================================
#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!

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

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

···

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

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?

···

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

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.

···

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

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.

···

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

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

···

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

oh and i know

#Capybara.default_driver = :selenium

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

···

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

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!

···

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

ive been trying to look up xpath and css selectors. i have my program
fill out the email and all i need to figure out now is to press the send
button. but its not like the other buttons i have used.

<table class="CLiKSTbl" width="100%" cellspacing="0" cellpadding="0"
border="0">
<td cellspacing="0" cellpadding="0">&nbsp;</td>
<tr><td class="CLiKSLineNmlbrown" colspan="7" width="100%"></td></tr>
    <tr cellspacing="0" cellpadding="0">
      <td class="CLiKStdleft" width="20%" cellspacing="0"
cellpadding="0">&nbsp;</td>
      <td class="CLiKStdleft" width="1%" cellspacing="0"
cellpadding="0">&nbsp;</td>
      <td class="CLiKStdleft" colspan="76%" cellspacing="0"
cellpadding="0" width="0%" >
        <Table width="0%" cellspacing ="0" cellpadding="0" border="0">

        <tr><td colspan="3">&nbsp;</td></tr>
        <tr>
          <td width="0%">&nbsp;</td>
          <td width="0%">
      <a
href="javascript:exit('/online/valdoc/ea_askaqsend?__i=1352473481343-4376714975339252553')"><img
src="/01/images/pg3_but2_new.gif" border="0" align="top"/></a>&nbsp;
            <!--
<TABLE BORDER=0 ><TR>
<TD><INPUT TYPE=BUTTON class="CliKSbttn" VALUE="Send"
onClick="javascript:exit('/online/valdoc/ea_askaqsend?__i=13524734813433775119952793994132')"></TD>
</tr></table>-->

that is the code from the pages source file. this is the send button i
need to click. but it says cannot find button or link with value send.
so i started looking into href, xpath, and css tried like 100 different
things and still no dice. any suggestions? what kind of button is it
would probably help a lot? i dont know how to tell if its css or xpath.

···

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

GOT IT!!!

find(:xpath, "//*[@class='CLiKStdleft']").click
find(:xpath, "//*[@src='/01/images/pg3_but2_new.gif']").click

theres the missing code! feels good now that it works though. thank you
for all your help!

···

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

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

···

2012/11/5 john smith <lists@ruby-forum.com>

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.

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

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

···

2012/11/5 john smith <lists@ruby-forum.com>

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

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

Start with ruby - how to run a standalone Capybara test? - Stack Overflow and look at
documentation at https://github.com/jnicklas/capybara\.

···

2012/11/5 john smith <lists@ruby-forum.com>

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?

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

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 <lists@ruby-forum.com>

oh and i know

#Capybara.default_driver = :selenium

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

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

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 <lists@ruby-forum.com>

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!

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

For button click on your page example this is correct way to click it:
find(:xpath, "//*[@id="sImage1"]").click

···

2012/11/9 john smith <lists@ruby-forum.com>

ive been trying to look up xpath and css selectors. i have my program
fill out the email and all i need to figure out now is to press the send
button. but its not like the other buttons i have used.

<table class="CLiKSTbl" width="100%" cellspacing="0" cellpadding="0"
border="0">
<td cellspacing="0" cellpadding="0">&nbsp;</td>
<tr><td class="CLiKSLineNmlbrown" colspan="7" width="100%"></td></tr>
    <tr cellspacing="0" cellpadding="0">
      <td class="CLiKStdleft" width="20%" cellspacing="0"
cellpadding="0">&nbsp;</td>
      <td class="CLiKStdleft" width="1%" cellspacing="0"
cellpadding="0">&nbsp;</td>
      <td class="CLiKStdleft" colspan="76%" cellspacing="0"
cellpadding="0" width="0%" >
        <Table width="0%" cellspacing ="0" cellpadding="0" border="0">

        <tr><td colspan="3">&nbsp;</td></tr>
        <tr>
          <td width="0%">&nbsp;</td>
          <td width="0%">
      <a

href="javascript:exit('/online/valdoc/ea_askaqsend?__i=1352473481343-4376714975339252553')"><img
src="/01/images/pg3_but2_new.gif" border="0" align="top"/></a>&nbsp;
            <!--
<TABLE BORDER=0 ><TR>
<TD><INPUT TYPE=BUTTON class="CliKSbttn" VALUE="Send"

onClick="javascript:exit('/online/valdoc/ea_askaqsend?__i=13524734813433775119952793994132')"></TD>
</tr></table>-->

that is the code from the pages source file. this is the send button i
need to click. but it says cannot find button or link with value send.
so i started looking into href, xpath, and css tried like 100 different
things and still no dice. any suggestions? what kind of button is it
would probably help a lot? i dont know how to tell if its css or xpath.

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

find(:xpath, "//*[@class='CLiKStdleft']").click

It could be written with CSS selectors:
find(:css, ".CLiKStdleft").click
or shorter (since CSS is default selector in Capybara):
find("CLiKStdleft").click