6

I am trying to auto submit form using webbrowser control. I am using the following code to submit"

currentElement.InvokeMember("submit");

Now this methods works fine. But sometimes a form may have some javascript function that is called on button click at the time of submission. So let's say if a form has some button image called "Submit" and when a user presses it, a javascript function somefunction() is called and then form is submitted.

Problem is when I use the above method InvokeMember then it only submits the form and doesn't execute associated scripts (in this case somefunction()) and I have to manually write code

webBrowser1.Document.InvokeScript("somefunction");

But this requires that I know before hand if there is some function. Is there any way I submit form and it will automatically run all associated javascript?

And I don't know button name or ID either which is clicked by user to submit form. Because in some cases it may not even have ID or name for e.g.

<span class="btn" onclick="somefunction()">
<img style="cursor:pointer" title="Submit" alt="Submit" src="http://stackoverflow.com/imagesbutton.png?2012">
<div id="s" style=""></div>
</span>
2
  • Oof. That <span onClick> example that you gave is going to make this a really tough problem to solve in the general case. In your <span onClick> example, I assume that typing stuff into the form and pressing <kbd>Enter</kbd> doesn't work, either. If the browser can't spot the submit button, I don't give your wrapper good odds... Commented Apr 26, 2012 at 18:02
  • You can simulate a click on anything but you can't determine this action eventually end up doing a submit. So, the answer to the question "Is there any way I submit form and it will automatically run all associated javascript?" is yes, provided you know where to click. Commented Apr 27, 2012 at 12:47

2 Answers 2

2
+50

It's been a while since I have messed around with the WebBrowser control, but I used to make it jump through hoops for me. I've come across this issue in the past.

http://www.codeproject.com/Tips/60924/Using-WebBrowser-Document-InvokeScript-to-mess-aro

When you get a Form object, take the string out of the OnSubmit and run this to execute it before submitting the form:

object[] codeString = {"myObject.setVariable(0);"};
webBrowser1.Document.InvokeScript("eval",codeString);

Works like a charm.

Sign up to request clarification or add additional context in comments.

Comments

2

If you know the name or id of the image tag you can use

webBrowser1.Document.GetElementById("button").InvokeMember("click");

or

webBrowser1.Document.GetElementByName("button").InvokeMember("click");  

to call the associate function in javascript

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.