views:

221

answers:

2

I try to submit a form by Mechanize, however, I am not sure how to add necessary form valuables which are done by some Javascript. Since Mechanize does not support Javascript yet, and so I try to add the variables manually.

The form source:

<form name="aspnetForm" method="post" action="list.aspx" language="javascript" onkeypress="javascript:return WebForm_FireDefaultButton(event, '_ctl0_ContentPlaceHolder1_cmdSearch')" id="aspnetForm">

<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/..." />

<script type="text/javascript">
<!--
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>

<script language="javascript">
<!--
var _linkpostbackhit = 0;
function _linkedClicked(id, key, str, a, b) {
    if (!b || !_linkpostbackhit) {
        if (!a) {
            __doPostBack(key, id);
            _linkpostbackhit = 1;
        } else {
            if (window.confirm(str)) {
                __doPostBack(key, id);
                _linkpostbackhit = 1;
             }
        }
     }
    return void(0);
}
// -->
</script>

...

<a href="JavaScript:_linkedClicked('123456','_ctl0:ContentPlaceHolder1:Link', '',0,1);">123456</a>

...

</form>

I tried to add the 2 variables:

page.forms.first['__EVENTTARGET']   = '_ctl0:ContentPlaceHolder1:Link'
page.forms.first['__EVENTARUGMENT'] = '123456'

and submit the form:

page.forms.first.click_button(page.forms.first.buttons.first)

The result returned only (re)show the current list of links as if I have not clicked on any of the links.

Any help will be appreciated. Thanks!

+1  A: 

When faced with this problem, I usually use Firefox and Firebug to find out how the request is made. Using the "Net" tab, you'll be able to see the request to "list.aspx" and all of its parameters.

agregoire
+1  A: 
page.forms.first['__EVENTARUGMENT'] = '123456' // -> should be '__EVENTARGUMENT'
Alsciende