title: Python - input data into form and submit hidden form
body: <p>Ok so this is my <strong>first post</strong> on StackOverflow, so go easy on me. I'm literally stuck with my <strong>Python</strong> script. I looked all over the web, cannot find a solution!</p>
  
  <p>So I used mechanize to login to a website (example: <em><a href="http://www.foobar.com/" rel="nofollow">http://www.foobar.com/</a></em>)</p>
  
  <p><strong>HTML of form to login:</strong></p>
  
  <p><code>form id="loginForm" method="post" action="/z/0.123/?cmd=login-post" onsubmit="return someSubmitfunction();"</code>                            </p>
  
  <p><strong>The form for the login of that website looks like this:</strong></p>
  
  <pre><code>    &lt;HiddenControl(__FOO=someLongString) (readonly)&gt;
      &lt;TextControl(emailAddress=)&gt;
      &lt;PasswordControl(password=)&gt;
      &lt;CheckboxControl(persist=[*on])&gt;
  </code></pre>
  
  <p>I WAS able to login to the website and redirect to an internal link (see further in code).</p>
  
  <p><strong>Here is the code for the login...</strong> Note: Request Method is a POST</p>
  
  <pre><code>    import urllib, urllib2
      import cookielib
      import mechanize
  
      # Note this is the FORM, but missing the HIDDEN value, LOOK lower in code
      EmailAddress = 'someusername'
      Password = 'somepassword'
      Persist = ['on',]
  
      browser = mechanize.Browser()
  
      # Enable cookie support
      cookiejar = cookielib.LWPCookieJar() 
      browser.set_cookiejar( cookiejar )
  
      # Browser options 
      browser.set_handle_equiv( True ) 
      browser.set_handle_redirect( True ) 
      browser.set_handle_referer( True ) 
      browser.set_handle_robots( False )
  
      # Pretend that I am a browser
      browser.set_handle_refresh( mechanize._http.HTTPRefreshProcessor(), max_time = 1 ) 
      browser.addheaders = [ ( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1' ) ]
  
      # Open webpage &amp; add form fields
      browser.open('http://www.foobar.com/')
      browser.select_form(nr = 0) #select the ONLY form (Login form)
      browser.form['emailAddress'] = EmailAddress
      browser.form['password'] = Password
      browser.form['persist'] = Persist
  
      # Submit for FORM is an action, find it and redirect to internal page
      # Create new control &amp; submit to internal page
      browser.new_control("HIDDEN", "action", {})
      control = browser.form.find_control("action")
      control.readonly = False
      browser["action"] = "/z/0.123/?cmd=login-post"
      browser.method = "post"
      browser.action = 'http://www.foobar.com/user/summary/'
  
      browser.submit()
  </code></pre>
  
  <p><strong>Alrighty, up to this point, I am fine.</strong> I SUCCESSFULLY logged in and was redirected to <em><a href="http://www.foobar.com/user/summary/" rel="nofollow">http://www.foobar.com/user/summary/</a></em> just like I wanted.</p>
  
  <pre><code>    url = browser.open('http://www.foobar.com/user/summary/')
      print url.read() # - see content of url HTML ### THIS WORKS ###
  </code></pre>
  
  <p>Now I use BeautifulSoup() to parse the HTML of /user/summary/ and successfully grab another form on this page. This form doesn't have an action, like the login form, but this is how it looks...</p>
  
  <p><strong>I NEED HELP FROM HERE down.. I have trouble inputting my text(myInput) into the form and submitting!</strong></p>
  
  <p><strong>HTML of form from .../user/summary/:</strong></p>
  
  <p><code>form method="post" id="foobar" name="foobar"></code></p>
  
  <p><strong>This is the submit button for the form:</strong></p>
  
  <p><code>onkeypress="return submitFormKey(event, '','foobar', 'foobar', 'pcm');">
  img src="someuglyimage.jpg" class="submit" id="btn_Submit" onclick="submitForm('foobar', 'foobar', 'pcm');" alt="Foo" </code></p>
  
  <p><strong>This is the actual form: (THE ONLY FORM on this page, once again!)</strong></p>
  
  <pre><code>    &lt;HiddenControl(hdnCmd=foobar) (readonly)&gt;
      &lt;TextControl(inputvalue=)&gt;
  </code></pre>
  
  <p>I tried many methods of submitting. I tried using Selenium, Splinter, urlib(1 &amp; 2), and even JSON, javascript, iframe, embed, etc. <strong>I'M STUCK, HELP PLZ!</strong></p>
  
  <p><strong>I thought this will work, i tried with and without Hidden control:</strong></p>
  
  <pre><code>    browser.select_form(nr = 0) #select the 1st form for inputting value
      browser.form['inputvalue'] = myInput #MY INPUT I WANT THERE
  
      browser.new_control("hidden", "foobar", {})
      control = browser.form.find_control("foobar")
      control.readonly = False
      #browser["foobar"] = "/?cmd=foobar&amp;from=/user/summary"
      browser.method = "post"
      response = browser.submit()
      print response.read()
  </code></pre>
  
  <p><strong>MY RESULTS:</strong></p>
  
  <p>Apparently It seems to redirect me to the homepage of the website (302 Redirect). So I KNOW that most likely it's something to do with the hidden value and passing it to the javascript/Ajax call (onclick="submitForm) when I submit. I read about CSRF tokens and it could be that, but if anyone has any ideas on how to do this, let me know because I'm in desperate need of help.</p>
  
  <p>And somehow I cannot find the form of .../user/summary/ (console tells me this) because I am redirected to the homepage, even though I dont submit the browser, until after I input all the form fields...</p>
  
  <p>I can read the HTML of .../user/summary/ and find the "foobar" form! This is why I am so confused. I can read it, parse it, but when i try to input myInput into the form, somehow I get redirected to homepage, but I am still logged in! </p>
  
  <p>THANKS pplz.. hopefully I was clear! </p>