Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging into a website using Mechanize and Nokogiri?

I am having some difficulty with one of our service providers login forms. The other sites are working fine but for some reason I can't get past their login form.

The website login for is like this:

<form accept-charset="UTF-8" action="/sessions" class="new_user_session" id="new_user_session" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="kaLEkPesQfeheronzGTdfnVAzpuUiC+VmjVXBu540n8=" /></div>

      <fieldset class="big">

      <div class="form-row">
        <div class="form-label">
        <label for="user_session_email">Email</label>
        </div>
        <div class="form-field">
        <input id="user_session_email" name="user_session[email]" size="30" type="text" />

        </div>

      </div>          

      <div class="form-row">
        <div class="form-label">
        <label for="user_session_password">Password</label>
        </div>
        <div class="form-field">
        <input id="user_session_password" name="user_session[password]" size="30" type="password" />

        </div>

        <div class="form-comment"><p><a href="/password_resets/new" class="link-password-recovery">Forgot your password?</a></p></div>
      </div>

        <div class="form-row optional">
          <div class="form-field">
            <label for="user_session_remember_me"><input name="user_session[remember_me]" type="hidden" value="0" /><input id="user_session_remember_me" name="user_session[remember_me]" type="checkbox" value="1" /> Remember me for 2 weeks</label>
          </div>

        </div>

</fieldset>  

I have tried to login using the same code as other the other sites but it doesn't work.

# Create a new mechanize object
agent = Mechanize.new

# Load the dial9 website
page = agent.get("http://webapplication.co.uk")

# Select the first form
form = agent.page.forms.first
form.username = 'username
form.password = 'password'

# Submit the form
page = form.submit form.buttons.first

I have also tried a different way of logging in as suggested in other SO questions/answers:

email = '[email protected]'
password = 'password

# Create a new mechanize object
agent = Mechanize.new

# Load the postmarkapp website
page = agent.get("https://domain.com")

# Select the first form
form = agent.page.forms.first
form.field_with(:email => "user_session_email").value = email
form.field_with(:password => "user_session_password").value = password

# Submit the form
page = form.submit form.buttons.first

Using this method of authentication I get the following output when running the rake task:

undefined method `email' for [hidden:0x3fef2ab2b994 type: hidden name: utf8 value: ✓]:Mechanize::Form::Hidden

Upon closer inspection the above error seems to be due to the fact that there is a field immediately after the form is started:

<form accept-charset="UTF-8" action="/sessions" class="new_user_session" id="new_user_session" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="kaLEkPesQfeheronzGTdfnVAzpuUiC+VmjVXBu540n8=" /></div>

Am I missing something? If so, what? Any pointers are appreciated!

like image 368
dannymcc Avatar asked Apr 01 '12 18:04

dannymcc


1 Answers

Try changing

form.field_with(:email => "user_session_email").value = email
form.field_with(:password => "user_session_password").value = password

to

form.field_with(:name => "user_session[email]").value = email
form.field_with(:name => "user_session[password]").value = password
like image 52
John Douthat Avatar answered Oct 11 '22 16:10

John Douthat