ArticleKey points: multi-thread processing of webbrowser-related issues, and solving the issue of triggering click events without JS response
Recently, we just met a requirement: to traverse web page elements cyclically and find the content specified by innertext, simulate manual clicks.
Solution: Use webbrowser to load documents at the specified position to the webbrowser control (note:The webbrowser is used to pull the control, rather than directlyCodeMedium new)
The source code is as follows:
Private Void Loadpage ( Object URL ){ Try { String Url = ( String ) URL; browser. navigate (URL ); While ( True ) {Application. doevents (); If (Browser. readystate! = Webbrowserreadystate. Complete ){ Break ;} Htmldocument document = Browser. Document; htmlelementcollection elems = Browser. Document. getelementsbytagname ( " A " ); Foreach (Htmlelement em In Elems ){ If (Em. innertext = " Test " ) {Em. invokemember ( " Click " ); // "Trigger Click Event" }}} Catch (Exception ex) {MessageBox. Show (ex. Message );}}
I did not mention that the above is only applicable to single threads. If the above code is executed in a newly opened thread, a series of problems will occur ...... (Unfortunately, I did not record those issues, but I believe that if you have the same requirements as me, that is, using multi-threaded processing of web page content loaded by webbrowser, you can continue reading it, which may be helpful to you)
One search on the Internet, the use of webbrowser in multithreading will encounter many problems ...... Later, I found several articles that helped me through Google (how far Baidu is from Google in this process:
Thread knowledge: http://www.cnblogs.com/JimmyZheng/archive/2012/06/10/2543143.html
Webbrowser multithreading brings troubles: Http://www.cnblogs.com/xjfhnsd/archive/2010/03/14/1685441.html
Webbrowser: 3-point details of HTML content:Http://www.cnblogs.com/cyq1162/archive/2012/03/27/2419655.html
Then, I used WebClient and webbrowser to Solve the Problem smoothly.
WebClient: Download the source code of a specified website
Webbrowser: navigate a blank page (For details, refer :)
Note:Here, the webbrowser should be new from the code. If it is created by pulling the control, an error will be reported: "The specified conversion is invalid"
Here I will write a document not mentioned above.
For example, the webpage element to be clicked is triggered by JS, while WebClient can only download the webpage source code of the specified URL, and cannot automatically load the relevant Js, therefore, when invokemember ("click") is used, the expected results are not displayed.
Solution: Download the specified JS file (or put the js method that needs to be used locally and load it using WebClient)
What I am talking about here is just a big question and a solution.
Finally, paste a piece of test code:
C # source code:
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. drawing; Using System. LINQ; Using System. text; Using System. Threading. tasks; Using System. Windows. forms; Using System. Threading; Using System. net; Namespace Webbrowser control { Public Partial Class Form1: FORM { Public Form1 () {initializecomponent ();} Private Void Button#click ( Object Sender, eventargs e) {control. checkforillegalcrossthreadcils = False ; Parameterizedthreadstart pt; PT = New Parameterizedthreadstart (loadpage); thread TD = New Thread (PT); TD. Name = " Main thread " ; TD. setapartmentstate (apartmentstate. Sta ); // Set the unit status before the thread starts. TD. Start ( " Http: // 127.0.0.1/test.html " );} Private Void Loadpage ( Object URL ){ Try { String Url = ( String ) URL; webbrowser Browser = New Webbrowser (); // The current thread is not in a single thread unit, so ActiveX control cannot be instantiated. Browser. scripterrorssuppressed = True ; // Browser. navigate (URL ); // Browser. documentcompleted + = browser_documentcompleted; Browser. navigate ( " About: blank " ); String Htmlcode = Gethtmlsource (URL ); String JS = gethtmlsource ( " Http: // 127.0.0.1/My/test/js.html " ); // Load JS Browser. Document. Write (JS); textbox1.text = Htmlcode; // While (browser. readystate! = Webbrowserreadystate. Complete) // The error "the specified conversion is invalid" is returned" // Application. doevents (); Browser. Document. Write (htmlcode ); // MessageBox. Show (browser. documenttext ); Htmldocument document = Browser. Document; htmlelementcollection elems = Browser. Document. getelementsbytagname ( " A " ); Foreach (Htmlelement em In Elems ){ If (Em. innertext = " Click " ) {Em. invokemember ( " Click " );// Alert ("trigger Click Event ") }}} Catch (Exception ex) {MessageBox. Show (ex. Message );} Finally { // MessageBox. Show ("current execution thread:" + thread. currentthread. Name ); }} // WebClient obtains the webpage source code Private String Gethtmlsource ( String URL ){ String Text1 = "" ; Try {WebClient WC = New WebClient (); text1 = WC. downloadstring (URL );} Catch (Exception ex) {MessageBox. Show (ex. Message );} Return Text1 ;}}}
Test.html:
< Html > < Head > < Title > </ Title > < Script Type = "Text/JavaScript" SRC = "A. js" > </ Script > </ Head > < Body > < A Href = "Javascript: void ();" Onclick = "T ()" > Click </ A > </ Body > </ Html >
JS:
<ScriptType= "Text/JavaScript">FunctionT () {alert ('Ddd');}</Script>
Original article, reproduced please indicate the source: http://www.cnblogs.com/hongfei/archive/2013/01/05/2846933.html