Reputation: 1841
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>
Upvotes: 6
Views: 2611
Reputation: 159
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
Upvotes: 2
Reputation: 3766
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.
Upvotes: 2