简体   繁体   中英

Mechanize Javascript

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!

Using mechanize-1.0.0 the following works:

 agent = Mechanize.new
 page = agent.get('http://127.0.0.1/some.aspx')

 form = page.form("aspnetForm")
 form.add_field!('__EVENTARGUMENT', 'Page$2')
 form.add_field!('__EVENTTARGET', 'ctl00$ContentPlaceHolder1$gvwSomeList')
 page = agent.submit(form) # this gets page 2

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.

page.forms.first['__EVENTARUGMENT'] = '123456' // -> should be '__EVENTARGUMENT'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM