Logging in using Mechanize and Nokogiri?

I am having difficulty with one of the forms of entering the service center. Other sites are working fine, but for some reason I can’t get past my login form.

The login for the site is as follows:

<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 tried to log in using the same code as other other sites, but it does not 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 also tried another login method, as suggested in other SO questions / answers :

email = 'user@domain.com'
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 authentication method, I get the following output when performing 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 after the start of the form, a field appears:

<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 yes, then? Any pointers are appreciated!

+5
1

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
+5

All Articles