# Having problem using Mechanize to log into Google Account

**URL:** <https://www.ruby-forum.com/t/having-problem-using-mechanize-to-log-into-google-account/106483>\
**Category:** Ruby\
**Created:** [June 11, 2007, 5:00am UTC](https://www.ruby-forum.com/t/having-problem-using-mechanize-to-log-into-google-account/106483 "2007-06-11T05:00:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [June 11, 2007, 5:00am UTC](https://www.ruby-forum.com/t/having-problem-using-mechanize-to-log-into-google-account/106483/1 "2007-06-11T05:00:36Z")

</div>

I am struggling with Mechanize to login to my Google account. Here is  
my code:

agent = WWW::Mechanize.new  
page = agent.get(‘[https://www.google.com/accounts/ServiceLogin?](https://www.google.com/accounts/ServiceLogin?)…’)

# or [http://www.google.com/calendar](http://www.google.com/calendar)

google\_form = page.forms[0]  
google\_form.fields.name(“Email”).first.value = ‘myaddress’  
google\_form.fields.find {|f| f.name == ‘Passwd’}.value = ‘mypasswd’

pp page  
results = agent.submit(google\_form, google\_form.buttons.first)  
pp page  
puts results.body

The values of the Email and Passwd fields remain empty. So it appears  
to me that the code fails to login. What more should I do? Please  
help!

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![codecarpenter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@codecarpenter](https://www.ruby-forum.com/u/codecarpenter)\
**Post date:** [June 11, 2007, 10:09am UTC](https://www.ruby-forum.com/t/having-problem-using-mechanize-to-log-into-google-account/106483/2 "2007-06-11T10:09:14Z")

</div>

Kevin,

> The values of the Email and Passwd fields remain empty. So it appears  
> to me that the code fails to login. What more should I do? Please  
> help!

Mechanize can not handle Javascript, so it won’t work this way. You have  
two options here:

1. Scrape the non-JS page - check this article:

[http://schf.uc.org/articles/2007/02/14/scraping-gmail-with-mechanize-and-hpricot](http://schf.uc.org/articles/2007/02/14/scraping-gmail-with-mechanize-and-hpricot)

1. Use Watir (FireWatir if you are not on win32) which can handle  
JavaScript.

btw, posting mechanize related questions to the mechanize list  
(removed\_email\_address@domain.invalid) can yield better response time/quality.

HTH,  
Peter  
\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby.
