# Login web page using mechanize

**URL:** <https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237>\
**Category:** Ruby\
**Created:** [November 4, 2012, 8:41pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237 "2012-11-04T20:41:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 4, 2012, 8:41pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/1 "2012-11-04T20:41:38Z")

</div>

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/](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!

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 4, 2012, 11:19pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/2 "2012-11-04T23:19:28Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 12:02am UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/3 "2012-11-05T00:02:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 10:13am UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/4 "2012-11-05T10:13:29Z")

</div>

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

2012/11/5 john smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 4:07pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/5 "2012-11-05T16:07:51Z")

</div>

It’s ok, but I prefer google groups or better [stackoverflow.com](http://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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 3:36pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/6 "2012-11-05T15:36:23Z")

</div>

“Иван Бишевац” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 6:01pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/7 "2012-11-05T18:01:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 6:35pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/8 "2012-11-05T18:35:10Z")

</div>

By the way, it’s not copybara but capybara 🙂

2012/11/5 john smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 6:40pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/9 "2012-11-05T18:40:03Z")

</div>

“Иван Бишевац” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #1082996:

> By the way, it’s not copybara but capybara 🙂
> 
> 2012/11/5 john smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 10:22pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/10 "2012-11-05T22:22:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 5:06pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/11 "2012-11-05T17:06:19Z")

</div>

On 4 Nov 2012, at 19:41, john smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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/](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.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 5, 2012, 11:34pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/12 "2012-11-05T23:34:28Z")

</div>

Start with [ruby - how to run a standalone Capybara test? - Stack Overflow](http://stackoverflow.com/a/4172101/507018) and look at  
documentation at [GitHub - teamcapybara/capybara: Acceptance test framework for web applications](https://github.com/jnicklas/capybara).

2012/11/5 john smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 6, 2012, 12:50pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/13 "2012-11-06T12:50:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 6, 2012, 12:07am UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/14 "2012-11-06T00:07:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 8, 2012, 6:33pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/15 "2012-11-08T18:33:00Z")

</div>

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/](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/](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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 6, 2012, 1:03pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/16 "2012-11-06T13:03:26Z")

</div>

“Иван Бишевац” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in post #1083041:

> Start with [http://stackoverflow.com/a/4172101/507018](http://stackoverflow.com/a/4172101/507018) and look at  
> documentation at [GitHub - teamcapybara/capybara: Acceptance test framework for web applications · GitHub](https://github.com/jnicklas/capybara).
> 
> 2012/11/5 john smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 8, 2012, 7:52pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/17 "2012-11-08T19:52:55Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 8, 2012, 8:02pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/18 "2012-11-08T20:02:34Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 8, 2012, 6:33pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/19 "2012-11-08T18:33:51Z")

</div>

oh and i know

#Capybara.default\_driver = :selenium

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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [November 8, 2012, 8:16pm UTC](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237/20 "2012-11-08T20:16:53Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[Next page](https://www.ruby-forum.com/t/login-web-page-using-mechanize/224237.md?page=2)
