Frequently Asked Questions¶
- How do I figure out what control names and values to use?
- What do those ‘*’ characters mean in the string representations of list controls?
- What do those parentheses (round brackets) mean in the string representations of list controls?
- Why doesn’t <some control> turn up in the data returned by .click*() when that control has non-None value?
- Why does mechanize not follow the HTML 4.0 / RFC 1866 standards for RADIO and multiple-selection SELECT controls?
- Why does .click() ing on a button not work for me?
- I’m having trouble debugging my code.
- I have a control containing a list of integers. How do I select the one whose value is nearest to the one I want?
Any Python 2.x version >= 2.7.0. mechanize does not currently support python 3.
mechanize.Browser’s response objects support the .seek() method, and can still be used after .close() has been called. Response data is not fetched until it is needed, so navigation away from a URL before fetching all of the response will truncate it. Call response.get_data() before navigation if you don’t want that to happen.
print(form) is usually all you need. In your code, things like the
HTMLForm.items attribute of
mechanize.HTMLForm instances can be
useful to inspect forms at runtime. Note that it’s possible to use item labels
instead of item names, which can be useful — use the by_label arguments to
the various methods, and the .get_value_by_label() / .set_value_by_label()
methods on ListControl.
A * next to an item means that item is selected.
Parentheses (foo) around an item mean that item is disabled.
Why doesn’t <some control> turn up in the data returned by .click*() when that control has non-None value?¶
Either the control is disabled, or it is not successful for some other reason. ‘Successful’ (see HTML 4 specification) means that the control will cause data to get sent to the server.
Why does mechanize not follow the HTML 4.0 / RFC 1866 standards for RADIO and multiple-selection SELECT controls?¶
Because by default, it follows browser behaviour when setting the initially-selected items in list controls that have no items explicitly selected in the HTML.
I have a control containing a list of integers. How do I select the one whose value is nearest to the one I want?¶
import bisect def closest_int_value(form, ctrl_name, value): values = map(int, [item.name for item in form.find_control(ctrl_name).items]) return str(values[bisect.bisect(values, value) - 1]) form["distance"] = [closest_int_value(form, "distance", 23)]
Use the developer tools for your browser (you may have to install them first). These provide excellent views into all HTTP requests/responses in the browser.
If you come across this in a page you want to automate, you have a few options. Here they are, roughly in order of simplicity:
- More complex is to use your browser developer tools to see exactly what requests are sent by the browser and emulate them in mechanize by using
mechanize.Requestto create the request manually and open it with