Mechanize form submit not going to the correct response page, no errors as to why. Something I'm doing?

Posted by Zack Shapiro on Stack Overflow See other posts from Stack Overflow or by Zack Shapiro
Published on 2013-10-17T15:52:22Z Indexed on 2013/10/17 15:53 UTC
Read the original article Hit count: 169

Filed under:
|
|
|

I threw this all in one controller for testing purposes.

My code fills out the form correctly for adding a new address to your Amazon account. There are two buttons that submit this form, one takes you to add a new address which is what I don't want, and the other is just a Save & Continue input/image.

When I submit the form via that button, as I do below, the form is still on the page, filled out as I have with my code. Inspecting the page titles, they're the same. There are no discernible errors that Mechanize or Amazon spit back.

Any ideas?

class AmazonCrawler
  def initialize
    @agent = Mechanize.new do |agent|
      agent.user_agent_alias = 'Mac Safari'
      agent.follow_meta_refresh = true
      agent.redirect_ok = true
    end
  end

  def login
    # login_url = "https://www.amazon.com/ap/signin?_encoding=UTF8&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fcss%2Fhomepage.html%3Fie%3DUTF8%26ref_%3Dgno_yam_ya"
    login_url = "https://www.amazon.com/gp/css/account/address/view.html?ie=UTF8&ref_=ya_add_address&viewID=newAddress"
    @agent.get(login_url)
    form = @agent.page.forms.first
    form.email = "[email protected]"
    form.radiobuttons.first.value = "0"
    form.radiobuttons.last.check
    form.password = "my_password"
    dashboard = @agent.submit(form)
  end
end

class UsersController < ApplicationController

  def index
    response = AmazonCrawler.new.login
    form = response.forms[1]

    # fill out form
    form.enterAddressFullName == "Your Name"
    form.enterAddressAddressLine1 = "123 Main Street"
    form.enterAddressAddressLine2 = "Apartment 34"
    form.enterAddressCity = "San Francisco"
    form.enterAddressStateOrRegion = "CA"
    form.enterAddressPostalCode = "94101"
    form.enterAddressPhoneNumber = "415-555-1212"
    form.AddressType = "RES"

    new_response = form.submit( form.button_with(value: /Save.*Continue/) )
  end

end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby