最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - Mechanize Javascript- Stack Overflow

programmeradmin1浏览0评论

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!

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!

Share Improve this question asked Apr 1, 2010 at 7:38 ohhoohho 52k76 gold badges261 silver badges387 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

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'
发布评论

评论列表(0)

  1. 暂无评论