Automatic Entry of complex website information

Source: Internet
Author: User
Problem: a certain system is based on B/S and cannot obtain data interfaces. There is an information entry page, which is based on showmodaldialog, after the initial information is entered, it will be redirect to another URL (assume that the original URL is url1 and the URL after redirection is url2 ), in url2, other information must be switched over the link through JavaScript (like the tab page effect). The input may be submitted with an alert message prompt, there is a pile of data to be processed (1000 records), which requires data Program Automatic Input.

Solution: Use extendedwebbrowser for processing. Use webbrowser to open url1, obtain the HTML document, automatically fill in the form, and then call the submit function. After Turning to url2, call the JavaScript function to switch to other information input pages, and automatically fill in the Form, call to save the JS function, after all the information of a record is input, turn to url1, start again, enter the next data.

Several technical points:
1. The page displayed in the showmodaldialog in the target system has no status bar and you cannot know about url1.
Solution: Use iehttpheaders to track showmodaldialog and obtain the actual value of url1.

2. You cannot determine whether the input page is loaded.
solution: add the Output E. URL statement to start your browser program developed based on extendedwebbrowser. Record the URL output after the form can be automatically filled in each step to form the feature URL of each step, in this way, you can process the pages loaded in frames, IFRAME, and Js. For example:
void brw_documentcompleted (Object sender, webbrowserdocumentcompletedeventargs e)
{< br> If (_ windowmanager. activebrowser. readystate = webbrowserreadystate. complete)
{< br> southhis. modoutputlog. outputlog (E. URL. tostring ();
}< BR >}< br> recorded are:
Step 1: http://xxx.xxx/BasicInfo.aspx? Opflag = add & strguardno =
Step 2: http://xxx.xxx/aaedit2.aspx
Step 2: http://xxx.xxx/aaedit3.aspx

3. errors may occur when entering the form in each step, but you need to repeat the steps. How can we make sure that the steps are executed to complete the previous step and then execute the next step and link them together.
Solution: Use the state machine solution, draw a state machine diagram, and define a variable to indicate the state machine state. After a successful step is executed, change the state variable to let the state machine execute the next action. If the execution fails, you can return and re-execute, or return to the previous step and re-load.

4. the execution speed of the program is too fast. Even if the program indicates that the page load is complete, when the form is automatically filled in, some onload tasks on the page are still not completed, resulting in subsequent forms or JS execution errors. that is, determine the URL of the last page after a step is "loaded" and execute the filling program. Sometimes there are still errors. How can this problem be solved.
Solution: multithreading is used to allow the steps to be executed on non-main threads. After the loading conditions are met, wait for a while. After the webbrowser is fully loaded, then use invoke to execute the filling program for each step.
Sample program:
Public class invokeobject
{
Public System. Threading. threadstart m_func;
Public mainform m_frm;
Public invokeaa (system. Threading. threadstart func, mainform FRM)
{
M_func = func;
M_frm = FRM;
}
Public void tothreadcall ()
{
Datetime dt1 = datetime. now;
While (true) // wait for "Loading completed" and then wait for 200 ms
{
Application. doevents ();
Datetime dt2 = datetime. now;
Timespan Ts = dt2.subtract (dt1 );
If (TS. milliseconds> 200)
{
Break;
}
}
M_frm.invoke (m_func); // execute the real step processing letter
}
}

UseCode:
Invokeaa abc = new invokeaa (gotofamilypage, this); // gotofamilypage is the step processing function
System. Threading. Thread Th1 = new system. Threading. Thread (ABC. tothreadcall );
Th2.name = "gotofamilypage ";
Th2.start ();

5. How to intercept the alert dialog box to continue.
Solution: Expand webbrowser to process showmessage. In extendedwebbrowser, add the Code:
// Intercept alert
# Region extendedwebbrowsersite
Class extendedwebbrowsersite: webbrowser. webbrowsersite, unsafenativemethods. idochostshowui
{
Webbrowser m_host;
Public extendedwebbrowsersite (webbrowser host)
: Base (host)
{
M_host = host;
}
Void response. idochostshowui. showmessage (ref unsafenativemethods. _ remotablehandle hwnd, string lpstrtext, string lpstrcaption, uint dwtype, string lpstrhelpfile, uint dwhelpcontext, out int plresult)
{
// Todo: the pop-up message box for custom processing
// Dwtype: 0x00000030 Problem
Southhis. modoutputlog. outputlog (lpstrtext );
Southhis. modoutputlog. outputlog (dwtype );
If (dwtype! = 0x00000030)
{

// plresult = (INT) system. windows. forms. messageBox. show (lpstrtext, lpstrcaption, (messageboxbuttons) dwtype);
}< br> plresult = 1;
}< br> void unsafenativemethods. idochostshowui. showHelp (ref unsafenativemethods. _ remotablehandle hwnd, string pszhelpfile, uint ucommand, uint dwdata, unsafenativemethods. tagpoint ptmouse, object pdispatchobjecthit)
{< br> // todo: pop-up help message box for custom processing
}< BR >}

Protected override webbrowsersitebase createwebbrowsersitebase ()
{
Return new extendedwebbrowsersite (this );
}

6. How to intercept the script error dialog box popped up when a JS error occurs
Solution: Before loading the page, use:
BRW. scripterrorssuppressed = true;
So that it does not display the dialog box.

7. How to Get frames, how to execute JavaScript, how to define additional JavaScript Functions, and how to get the return values of executed JavaScript Functions.
Solution:
Htmlwindow win1 = _ windowmanager. activebrowser. Document. Window. Frames [0];
Mshtml. ihtmlwindow2 win2 = (mshtml. ihtmlwindow2) win1.domwindow;
Try
{
String script11 = "function saverow11 () {return '0 ';}";
Win2.execscript (script11, "JavaScript ");
String strretval = (string) win1.document. invokescript ("saverow11 ");
If (strretval = '0 ')
{
....
}
} Catch .....

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.