Question

In the script I wrote, I'm logging into Amazon successfully on behalf of the user with their credentials. Then I'm trying to fill out the form to enter a new address. Here's my current code:

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/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 = "whatever@gmail.com"
    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"
    form.enterAddressIsDomestic = "0"
    form.GateCode = ""

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

end

I submit the form and it takes me to the same "Add an Address" form but there are no discernible errors, nothing new on the page. The new_response request has the same form filled out.

When I inspect the post request, here's what I get:

enterAddressFullName=test+user&enterAddressAddressLine1=123+Main+Street&enterAddressAddressLine2=123&enterAddressCity=san+francisco&enterAddressStateOrRegion=ca&enterAddressPostalCode=94101&enterAddressCountryCode=US&enterAddressPhoneNumber=4155551212&enterAddressIsDomestic=0&AddressType=RES&GateCode=&isDomestic=0&newAddress.x=55&newAddress.y=17&newAddress=Save+%26+Continue&addressID=&sessionId=204-4423391-1593712

The main thing that stands out to me is the last part of it:

&newAddress.x=55&newAddress.y=17&newAddress=Save+%26+Continue&addressID=&sessionId=176-3067966-1293712

I'm not setting newAddress.x or newAddress.y anywhere, I seem to be unable to.

Any ideas as to why this isn't submitting successfully? Is it an SSL issue perhaps?

Was it helpful?

Solution 2

Figured it out. I was setting the name with the == rather than = which was causing the field to be empty

OTHER TIPS

newAddress is the name of the button. The .x and .y indicate where exactly on the button was clicked. There's a lot of reasons why a form will return you back, most likely it didn't like one of your values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top