无法通过Mechanize(Ruby)提交Amazon表单

时间:2013-10-18 22:13:05

标签: ruby-on-rails ruby ruby-on-rails-3 mechanize mechanize-ruby

在我写的脚本中,我代表用户使用他们的凭据成功登录亚马逊。然后我正在尝试填写表格以输入新地址。这是我目前的代码:

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

我提交表单,它会将我带到相同的“添加地址”表单,但没有可识别的错误,页面上没有新内容。 new_response请求填写了相同的表单。

当我检查帖子请求时,这是我得到的:

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

对我来说最重要的是它的最后一部分:

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

我没有在任何地方设置newAddress.xnewAddress.y,我似乎无法做到。

关于为什么这不能成功提交的任何想法?这可能是SSL问题吗?

2 个答案:

答案 0 :(得分:0)

newAddress是按钮的名称。 .x和.y表示单击按钮的确切位置。表格会让你回归的原因很多,很可能它不喜欢你的一个价值观。

答案 1 :(得分:0)

想出来。我使用==而不是=来设置名称,这导致该字段为空