简体   繁体   中英

How to auto submit javascript form

There is a search button aa webpage :

<td><table border="0" cellpadding="0" cellspacing="0"><tr><td dir="ltr" width="10" height="21"><img src="/global/images/ButtonLeftDove.gif" border="0" alt="" height="21" width="10" /></td><td height="21" align="center" valign="middle" class="ButtonDove" nowrap="nowrap"><a href="javascript:__doPostBack('mobjTemplate$ctl01$btnSearch2','')" onmouseover="window.status='Search';return true;" onmouseout="window.status='';return true;">Search</a></td><td dir="ltr" width="10" height="21"><img src="/global/images/ButtonRightDove.gif" border="0" alt="" height="21" width="10" /></td></tr></table></td>

it is calling a javascript function javascript:__doPostBack('mobjTemplate$ctl01$btnSearch2','')

the java function is as following:

function __doPostBack(eventTarget, eventArgument) {

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

        theForm.__EVENTTARGET.value = eventTarget;

        theForm.__EVENTARGUMENT.value = eventArgument;

        theForm.submit();

    }

}

I need to auto click that search button.

I tried this code but it does not work:

HtmlElementCollection elemColl2 = null;
HtmlDocument doc = webBrowser.Document;
elemColl2 = doc.GetElementsByTagName("form");

foreach (HtmlElement elem in elemColl2)
            {
                elem.InvokeMember("submit");
            }

How can I do that??

Thanks for help in advance..

I would suggest trying to get the anchor (or button) and use .click() on that. I've just had poor luck using 'submit' on the form itself in javascript in the past (which is what you're doing, through a layer of indirection).

You can use:

link.InvokeMember("Click");

To click the link once you've gotten it into a variable.

I suspect the reason that it's not "submitting" is because the element in the page isn't a form element and doesn't have an associated submit action. It's an anchor element with some JavaScript in the href (which is distasteful to say the least, but it's not your code so don't worry).

(Side note, you keep saying "Java" where you mean "JavaScript." The two are quite different.)

This answer to a very similar (though not exactly duplicate, so I'm hesitant to vote to close this question) question appears to be a workable solution. You may need to add a COM reference, which I personally try to avoid unless absolutely necessary (but that's just me), but being able to invoke a "click" on the anchor element in question should do the trick. (Assuming the WebBrowser control is interpreting the JavaScript on the page correctly.)

You can get the button element with javascript and then call the click event manually. And... I will rather stop programming like that (I mean calling asp.net ids in javascript. There's so much easier ways to do the same. Eg: a submit button

Understand the fact that the code you write in C# is going to be executed at the server side. What you actually need is to write a javascript code so that that form get submitted at client side. you can call form.submit when you want to submit the form.

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