jjlee / mechanize

Stateful programmatic web browsing in Python, after Andy Lester's Perl module WWW::Mechanize .
http://wwwsearch.sourceforge.net/mechanize/
618 stars 121 forks source link

Add an easy way to submit forms without "clicking" on control #32

Open ghost opened 13 years ago

ghost commented 13 years ago

If you automate an ASP.NET site quite often you have to "emulate" javascript handlers in your python code. I have seen a couple of cases then submit should be done after clicking on A tag at the same time form has a clickable control.

Even if I update required hidden controls (__EVENTTARGET) and do browser.form.submit() without arguments, mechanize "emulates" click on the first clickable control and I got wrong result.

It would be very useful if I can use some special argument(s) value to HTMLForm.click which will result in running HTMLForm._switch_click method even if there clickable controls in the form.

jjlee commented 13 years ago

I don't follow that -- do you have an example?

ghost commented 13 years ago

Ok. Here is non-working example:

import mechanize
from BeautifulSoup import BeautifulSoup

br = mechanize.Browser()

br.set_handle_robots(False)
# We are Internet Explorer 6.01
br.addheaders = [
    ('User-agent', 'Mozilla/4.0 (compatible; MSIE 6.01; Windows NT 6.0)')
]

br.open(r'http://softwarestore.ispfulfillment.com/store/Product.aspx?skupart=I23S14')

br.select_form('aspnetForm')
br.submit('ctl00$MainContent$ddlTypeProduct$db_ddlTypeProduct')

html = br.response().read()
page = BeautifulSoup(html)

option = page.find('a', 'ddlTypeProduct_MenuItem')
target = option['href'].split("'")[1]

br.select_form('aspnetForm')

event_target = br.form.find_control("__EVENTTARGET")
event_target.readonly = False
event_target.value = target

br.submit()

on the last line mechanize submit the form by emulating click on the first clickable control in the form (input with type='image' and name='ctl00$MainContent$ddlTypeProduct$db_ddlTypeProduct'). As a result server sends me wrong page.

I need a clear way to simulate submit from javascript, i.e. without clicking on any controls.

Hope this description is better than the first one...