WebBrowser easy to automate form filling

Source: Internet
Author: User

14003589

WebBrowser easy to automate form filling

The steps are as follows:
First step: Get the Navigate series function of the page call WebBrowser. Wait for the Web page to finish loading and get the Document object. Before calling Webbrowser.document, make sure that the WebBrowser Web page is loaded and can be monitored through webBrowser documentcompleted events. Tips to specify a variable first
The steps are as follows:

First step: Get the Web page
Call WebBrowser's Navigate series function. Wait for the Web page to finish loading and get the Document object.

Before calling Webbrowser.document, make sure that the WebBrowser Web page is loaded and can be monitored through webBrowser documentcompleted events.

Little Tricks

Specify a variable first:
IsCompleted
Set IsCompleted to False before calling the Navigate,refresh series function of WebBrowser.
In the DocumentCompleted event, if ReadyState is Webbrowserreadystate.complete (a value of 4), set IsCompleted to True.

The iscompleted is judged before calling Webbrowser.document. If it is not true, the delay loop waits until iscompleted is set to true.

Step two: Set page element data

Make sure that you get the Document object and get the page elements through getElementById, such as:

HtmlElement user = WebBrowser.Document.GetElementById ("user");
HtmlElement password = webBrowser.Document.GetElementById ("password");

Set page element values:

User. InnerText = "MyName";
Password. InnerText = "123456";

Little Tricks
function SetAttribute set dropdown box, check box, multi box data

Drop-down box:
Secret. SetAttribute ("Value", "Question1");
check box
RememberMe. SetAttribute ("Checked", "True");
Multi-Select box
Cookietime. SetAttribute ("Checked", "checked");

Of course, before the setattribute, you can also use the getattribute to determine the relevant properties, and then the root needs to be set.


Step three: Submit Web page data

The button object for the commit is first taken.
HtmlElement submit = WebBrowser.Document.GetElementById ("submit");

To submit
Submit. InvokeMember ("click");

is how simple. The entire process is the process of HTTP GET and post.

Of course, you can also use low-level WebRequest, there are more low-level socket to do, but now the numerous pages have such, such a deformation verification code, if you and I like, do not have the verification code identification, and covet the quick task, the above approach is absolutely fast and good.

WebBrowser traversing a Lookup element by property (tag)

function Getdoclinkbyhref (doc:ihtmldocument2;str_url:string): ihtmlanchorelement;

Var
I:integer;
Label_idispatch:idispatch;
Label_element:ihtmlanchorelement;
Begin
Result: = nil;

Doc: = Webbrowser1.document as Ihtmldocument2;
If not Assigned (DOC) Then
Exit;
For i:=0 to Doc.links.length-1 do
Begin
Label_idispatch: = Doc.links.item (i,0);
Label_element: = Doc.links.item (i,0) as ihtmlanchorelement;
if (Ansipos (Str_url), (Doc.links.item (i,0) as IHTMLElement). getattribute (' tag ', 0))
Then
Begin
Result: = label_element;
break;
End
End
End

WebBrowser easy to automate form filling

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.