Home > Back-end >  How to automatically fill in web forms, and submit automatically
How to automatically fill in web forms, and submit automatically

Time:10-23

My idea is complete simulation of artificial operation, but encountered a difficult: how to quickly locate web page input box, in addition, this method of simulation of the manual operation efficiency is low, and a higher efficiency of method

CodePudding user response:

To implement the HTTP protocol, there are examples of HTTP WebClnt, you look at:
http://download.csdn.net/detail/geoff08zhang/4571358

CodePudding user response:

To find the form in the HTML code, such as input, the form of the structure is extracted, and then generate a GET or POST request to the server

CodePudding user response:

This is easy to implement in c #, use the BCB to invoke ihtml API is more complex, a c # information for you.
In.net 2.0, the HTML document and it contains all the HTML element, and each HtmlDocument, HtmlElement. Such as.net object, so as long as find the "close" button corresponding to the HtmlElement object, add the Event to the click Event Handler,
HTML code
<body>
Then find out the button and add the Event Handler code is as follows:
C # codeHtmlDocument htmlDoc=webBrowser. Document; HtmlElement btnElement=htmlDoc. All [" btnClose "]; If (btnElement! +==null) {btnElement. Click new HtmlElementEventHandler (HtmlBtnClose_Click); }
The HtmlBtnClose_Click is Web button when the Event Handler,
Very simple? Then a little a bit more advanced - we all know that an HTML element may have a lot of all kinds of events, and HtmlElement this class given only the most commonly used, common a few, so, how to respond to other events? This is also very simple, just call the HtmlElement AttachEventHandler ok:
C # codebtnElement. AttachEventHandler (" onclick ", new EventHandler (HtmlBtnClose_Click));//this is equivalent to the above btnElement. Click +=new HtmlElementEventHandler (HtmlBtnClose_Click);
For other events, change "onclick" to name the event, such as:
C # code
FormElement. AttachEventHandler (" onsubmit ", new EventHandler (HtmlForm_Submit));
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
To make our WebBrowser autofill, even the function of automatically submit, is not difficult,
Suppose there is one of the most simple login page, enter the user Name password, login point "login" button, the known user Name input box id (or Name, similarly hereinafter) is the username and password input box id is password, id is submitbutton "login" button, so we only need to use the following code in the webBrowser DocumentCompleted event can be:
C # codeHtmlElement btnSubmit=webBrowser. Document. All [" submitbutton "]; HtmlElement tbUserid=webBrowser. Document. All (" username "); HtmlElement tbPasswd=webBrowser. Document. All (" password "), If (tbUserid==null | | tbPasswd==null | | btnSubmit==null) return; TbUserid. SetAttribute (" value ", "smalldust"); TbPasswd. SetAttribute (" value ", "12345678"); BtnSubmit. InvokeMember (" click ");
Here we use the SetAttribute to set the text frame's "value" attribute, use InvokeMember method to invoke the button of "click", because different Html element, its properties and methods are also different, so. Net 2.0 provides a unified HtmlElement summed up all kinds of Html elements at the same time, provides the function of these two methods by calling elements unique, on the Html element properties and methods of a browser, you can refer to the MSDN DHTML Reference,
Specified on the form submit, indeed there is another way is to get the form elements instead of the button, and they submit form element method:
C # codeHtmlElement formLogin=webBrowser. Document. The Forms (" loginForm ");//... FormLogin. InvokeMember (" submit "); This article did not recommend this method because the web page now, many of them on the submit button to add the onclick event, to do the most basic authentication, the content of the submitted if directly using the form submit method, the verification code is enforced, is likely to cause an error
  • Related