Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# webbrowser control (executing all javascripts associated with form)

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>
like image 306
Ali Avatar asked Apr 23 '12 07:04

Ali


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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.

like image 156
Derreck Dean Avatar answered Oct 04 '22 20:10

Derreck Dean


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

like image 28
carminePat Avatar answered Oct 04 '22 20:10

carminePat