Mechanize Javascript

喜欢而已 提交于 2019-12-21 21:29:26

问题


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:


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



回答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.




回答3:


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


来源:https://stackoverflow.com/questions/2558856/mechanize-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!