# Need help translating Ruby code

**URL:** <https://www.ruby-forum.com/t/need-help-translating-ruby-code/148087>\
**Category:** Ruby\
**Created:** [September 9, 2008, 5:35am UTC](https://www.ruby-forum.com/t/need-help-translating-ruby-code/148087 "2008-09-09T05:35:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [September 9, 2008, 5:35am UTC](https://www.ruby-forum.com/t/need-help-translating-ruby-code/148087/1 "2008-09-09T05:35:12Z")

</div>

Hi I am new to Ruby from Python and am still learning some stuff. I  
have a Ruby program that uses the mechanize library to connect to a Web  
site and authenticate with a username and password. The issue I have is  
that I need to get this code translated into Python. The Python  
mechanize library is not very well documented and I was wondering if  
anyone here had any experience using this library in Ruby or Python.  
Basically the only tricky part of this program is that it needs to add a  
field to the form on the page. This extra field, “\_\_EVENTTARGET”, needs  
to be added explicitly. I cannot find the Python equivalent to the  
WWW::Mechanize::Form::Field.new(nil,nil) method. Thank you in advance.  
I hope I explained myself well.

```auto
require 'rubygems'
require 'mechanize'

Hpricot.buffer_size = 262144

auth = {
  :username => "removed_email_address@domain.invalid",
  :password => "letmeinalready"
}

login_url = "http://www.princetonreview.com/Login3.aspx?uidbadge=" # !>
`&' interpreted as argument prefix
agent = WWW::Mechanize.new
page = agent.get( login_url )
my_form = page.forms.first

my_form['ctl00$MasterMainBodyContent$txtUsername']= auth[:username]
my_form['ctl00$MasterMainBodyContent$txtPassword']= auth[:password]

target_field = WWW::Mechanize::Form::Field.new(nil,nil)
target_field.name = "__EVENTTARGET"
target_field.value = "ctl00$MasterMainBodyContent$btnLogin"
my_form.fields << target_field

new_page = my_form.submit

# examine output to make sure it worked
new_page = agent.get("http://www.princetonreview.com/YourStuff.aspx")

pp new_page
```

---

<div class="post-metadata">

**Author:** ![kturner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c2a13f/32.png) [@kturner](https://www.ruby-forum.com/u/kturner)\
**Post date:** [September 9, 2008, 5:01pm UTC](https://www.ruby-forum.com/t/need-help-translating-ruby-code/148087/2 "2008-09-09T17:01:15Z")

</div>

On Mon, Sep 8, 2008 at 9:28 PM, Kevin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I hope I explained myself well.  
> auth = {  
> my\_form[‘ctl00$MasterMainBodyContent$txtUsername’]= auth[:username]
> 
> # examine output to make sure it worked
> 
> new\_page = agent.get(“[http://www.princetonreview.com/YourStuff.aspx](http://www.princetonreview.com/YourStuff.aspx)”)
> 
> ## pp new\_page [/code]
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I think this would be a better question for a Python list as you want a  
Python answer.

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)
