Max
Max

Reputation: 65

Python Mechanize - How to submit an unlisted value in dropdown menu

I am using Python's mechanize to add items into an Amazon shopping cart. On an item's product page, you select the Quantity in the form's dropdown menu and submit Add to Cart.

The dropdown menu allows you to select Quantities from 1 through 30.

The following code works for adding any product with quantities 1 through 30. However, it does not work when I attempt to add a Quantity greater than 30, i.e. when the value is not specifically expressed in the dropdown menu).

The max value of 30 in the dropdown menu is an artificial constraint. You can in fact add up to 999 items without a problem (using Firebug to submit a value greater than 30 confirms this).

My question - how can I change the following code to successfully submit a quantity NOT listed as a value in the dropdown menu?

import mechanize

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]

# Open an Amazon product page
br.open('http://www.amazon.com/gp/product/B005KOKFR4/')

# Add to Cart is form [1]
br.select_form(nr=1)

# Change quantity
br.form['quantity'] = ['31']

# Submit form
br.submit()

print br.response().read()

If quantity is from 1-30, the above code works. When quantity is 31 or more, the error is:

mechanize._form.ItemNotFoundError: insufficient items with name '31'

Upvotes: 3

Views: 342

Answers (1)

Matt
Matt

Reputation: 101

It seems that even when I try to manually put in a number greater than 30 it doesn't allow me to. Are you positive that it is allowed up to 999? Perhaps 30 is the maximum order size?

Upvotes: 0

Related Questions