Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the url of the response page after I've submitted a form in mechanize?

Using mechanize (and python) I can go to a website, log in, find a form, fill in some answers, and submit that form. However, I don't know how I can open the "response" page - that is, the page that automatically loads once you've submitted the form.

Here's the python code:

br.select_form(name="simTrade")
br.form["symbolTextbox"] = "KO"
br.form["quantityTextbox"] = "10"
br.form["previewOrderButton"]

preview = br.submit()
print preview.read

With the above code, I can see what the response page holds. But I want to actually open that page and interact with it. How can I do that with mechanize? Thank you.

EDIT: So I answered my own question soon after posting this. Here's the code:

br.select_form(name="simTrade")
br.form["symbolTextbox"] = symbol
br.form["transactionTypeDropDown"] = [order_type]
br.form["quantityTextbox"] = amount
br.form["previewOrderButton"]

no_url = br.submit()
final = no_url.geturl()
x = br.open(final)
print x.read()

To get the html source code of the response page (the page that loads when you submit a form), I simply had to get the url of br.submit(). And there's a built in mechanize function for that, geturl().

like image 555
user1822552 Avatar asked Mar 02 '14 21:03

user1822552


1 Answers

The OP's answer is a bit convoluted and resulted in a AttributeError. This worked better for me:

br.submit()
base_url = br.geturl()
print base_url
like image 114
otteheng Avatar answered Nov 13 '22 05:11

otteheng