WebBrowser Fetch page Verification code

Source: Internet
Author: User

Encounter an incomparable pit father, plus egg pain lactic acid problem. From the last night to find a bug, to solve the problem this afternoon, the majority of days of time. It takes a long time to find a problem, the solution is simple, but the formation of the solution is a very tangled process.

Background: WebBrowser Gets the verification code on the page, and then resolves the captcha back to the page. Parsing and writeback are no problem, the main problem is to get the page verification code.

Searching the web for this piece of code:

IHTMLControlElement img = (ihtmlcontrolelement) webbrowser1.document.images["vcode_img"]. Domelement;ihtmlcontrolrange Range=(Ihtmlcontrolrange) ((HTMLBody) html.body). createControlRange (); Range.add (IMG); Range.execcommand ("Copy",false,NULL); IMG=NULL; Range=NULL; HTML=NULL;if(Clipboard.containsimage ()) This. pictureBox1.Image =clipboard.getimage ();ElseMessageBox.Show ("Unsuccessful Execution"); Clipboard.clear ();

The core of getting a CAPTCHA picture is also here. Copy the picture data into memory, and then read from the Clipboard to the PictureBox control.

The picture came out very smoothly.

However, when the actual call, found "when the spirit is not good", occasionally will appear "unsuccessful execution" of the Bullet box.

At first I thought it was the thread running order, and added a bunch of thread.sleep and found no improvement.

Various tests, no clue ...

When you inadvertently find an error, some text is selected on the page. Is this the problem here? So the key test, each time the text is selected, parse the verification code, error. And without the text selected, run this part of the code, Enron passed. I x, what is this dog blood situation, the problem actually out on the page currently has the selected text?? Depressed na .....

Once the problem is found, the direction of the solution is clear. Cancel the page selection before executing this code.

Problem again, for me such a did not have a JS, HTML, C # Primary Ape, how to cancel WebBrowser page selection text is really difficult to me, tried many methods, focus,select,movestart,move ... All the drops of death ...

The process was bumpy and the results were concise. A sentence of Txtrange.execcommand ("unselect");

Full code:

varCode ="";varBODY = (mshtml. Htmlbodyclass) (MSHTML. Htmldocumentclass) wbscontent.document.window.frames[0]. document.domdocument). body;ihtmlcontrolelement img= (ihtmlcontrolelement) ((mshtml. Htmldocumentclass) wbscontent.document.window.frames[0]. document.domdocument). Images.item ("Imgcode"); Ihtmlcontrolrange Range=(Ihtmlcontrolrange) Body.createcontrolrange (); IHTMLTxtRange Txtrange=Body.createtextrange (); Txtrange.execcommand ("unselect"); Range.add (IMG); Range.execcommand ("Copy"); IMG=NULL; Range=NULL;if(Clipboard.containsimage ()) {Code=Uncodebase.getvarifycodefromwinwintask ((Bitmap) clipboard.getimage ()); varTXTVRF = (mshtml. Htmlinputelementclass) (MSHTML. Htmldocumentclass) wbscontent.document.window.frames[0]. document.domdocument). All.item ("Code"); Txtvrf.setattribute ("value", code); return true;}

This is the case of frame, I believe that the situation without frame should be able to follow this approach.

The problem solved, still very happy, although still feel that this problem is very frustrating.

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.