C # webbrowser control (execution of all javascripts related to form)

I am trying to automatically submit a form using the webbrowser control. I use the following code to send "

currentElement.InvokeMember("submit"); 

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

The problem is that I use the above InvokeMember method, then it only submits the form and does not execute the associated scripts (in this case somefunction ()), and I have to manually write the code

 webBrowser1.Document.InvokeScript("somefunction"); 

But this requires that I know before myself if there is any function. Is there a way to submit the form and it will automatically launch all the related javascript?

And I do not know the name of the button or the identifier that the user clicked to submit the form. Since in some cases it may not even have an identifier or name, for example,

 <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> 
+6
source share
2 answers

It has been a while since I came across a WebBrowser control, but I used it to go through the hoops for me. I have come across this question in the past.

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

When you get the Form object, output the line from OnSubmit and run it to execute it before submitting the form:

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

It works like a charm.

+2
source

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 an associated function in javascript

+2
source

Source: https://habr.com/ru/post/913902/


All Articles