How to fill out login form with mechanize in Ruby?
The following code should work:
page = agent.get("your_page_url")
form = page.form_with(:id => 'form-login-page')
form.login = "my_login"
form.password = "my_password"
form.submit
Rails ruby-mechanize how to get a page after redirection
I found problem cases that MIMS site auto submit form with page onload callback for checking authentication. It is not working with machanize gem.
Solution
Manually submitting the form two times solves this issue. Example
url = 'http://www.mims.com/India/Browse/Alphabet/A?cat=drug'
page = agent.get url # here checking authentication if success then redirecting to destination
p page
page.form.submit
agent.page.form.submit
Haskell IO and closing files
As others have stated, it is because of lazy evaluation. The handle is half-closed after this operation, and will be closed automatically when all data is read. Both hGetContents and readFile are lazy in this way. In cases where you're having issues with handles being kept open, typically you just force the read. Here's the easy way:
import Control.Parallel.Strategies (rnf)
-- rnf means "reduce to normal form"
main = do inFile <- openFile "foo"
contents <- hGetContents inFile
rnf contents `seq` hClose inFile -- force the whole file to be read, then close
putStr contents
These days, however, nobody is using strings for file I/O anymore. The new way is to use Data.ByteString (available on hackage), and Data.ByteString.Lazy when you want lazy reads.
import qualified Data.ByteString as Str
main = do contents <- Str.readFile "foo"
-- readFile is strict, so the the entire string is read here
Str.putStr contents
ByteStrings are the way to go for big strings (like file contents). They are much faster and more memory efficient than String (= [Char]).
Notes:
I imported rnf from Control.Parallel.Strategies only for convenience. You could write something like it yourself pretty easily:
forceList [] = ()
forceList (x:xs) = forceList xs
This just forces a traversal of the spine (not the values) of the list, which would have the effect of reading the whole file.
Lazy I/O is becoming considered evil by experts; I recommend using strict bytestrings for most of file I/O for the time being. There are a few solutions in the oven which attempt to bring back composable incremental reads, the most promising of which is called "Iteratee" by Oleg.
Related Topics
Running Selenium::Webdriver::Firefox Inside Xvfb from Ruby as Non-Root User
Why Can't I Create an Array as a Column in a Table in Rails
Ruby Ternary Operator Structure
Single Table Inheritance to Refer to a Child Class with Its Own Fields
In Rspec, Using Let Variable Inside Before :All Block
Flash Message with HTML_Safe from the Controller in Rails 4 (Safe Version)
Rails Activerecord Create or Find
"Dyld: Library Not Loaded", "Libruby.1.9.1.Dylib" When Calling "Mvim ." in the Command Line
Overriding Model in Gem, Adding Callback and Methods
Ssl_Connect Error When Accessing Shopify API with Rubygem
Ruby or Rails Sort on Two/Multiple Date Fields
Nanoc Changing the Base Path When Deploying Page in Github
How to Test Whether a String Would Match a Glob in Ruby
Ruby: How to Call Function Before It Is Defined
Garbage Collector in Ruby 2.2 Provokes Unexpected Cow
What Are the Meanings of the Hash Keys When Calling Objectspace.Count_Objects