简体   繁体   中英

Get Captcha Image from Web Browser control without using SRC

I know this question might sound familiar and there are plenty of posts out there on google with the same title BUT trust me this is different.

Editor : VS2008 (cannot upgrade it due to some technical difficulties)

Question

How to get Captcha Image from a Web Browser without using SRC ?

Why wouldn't you use SRC?

Here is the site from which i am trying to get my Captcha Image https://services.gst.gov.in/services/login
(The capta image appears once you type anything in User Name)

Now if you right click on the Captcha Image and go to inspect element you will see that the SRC of the captcha is:-

在此处输入图片说明

https://services.gst.gov.in/services/captcha?rnd=0.5313315062651027

and whenever you try to go to that link it will give you a captcha that is different from the previous one. That is why i cant use the below code because it shows different captcha than the one showing in the WebBrowser right now.

HtmlElement element = webBrowser1.Document.GetElementById("imgCaptcha");
string src = element.GetAttribute("src");
pictureBox1.Load(element.GetAttribute("src"));

You can use createControlRange to create a controlRange of non-text elements. Then find the image tag, for example by using id , then add the image tag to the control range and call it's execCommand method to execute Copy command, and finally, get the image from clipboard:

.NET 3.5

Add a reference to MSHTML . You can find it by Microsoft HTML Object Library under COM references and then add using mshtml; . Then:

IHTMLElement2 body = (IHTMLElement2)webBrowser1.Document.Body.DomElement;
IHTMLControlRange controlRange = (IHTMLControlRange)body.createControlRange();
IHTMLControlElement element = (IHTMLControlElement)webBrowser1.Document
    .GetElementById("imgCaptcha").DomElement;
controlRange.add(element);
controlRange.execCommand("Copy", false, null);
pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap); 

.NET >= 4.0

You don't need to add a reference, you can take advantage of dynamic :

dynamic body = webBrowser1.Document.Body.DomElement;
dynamic controlRange = body.createControlRange();
dynamic element = webBrowser1.Document.GetElementById("imgCaptcha").DomElement;
controlRange.add(element);
controlRange.execCommand("Copy", false, null);
pictureBox1.Image = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap);

Note:

  • Run the code when the document is completed, for example in DocumentCompleted event.

  • Also you may want to add null checking to the code.

  • I used above code to get the google logo from https://www.google.com by id hplogo .

  • I also tested above code, by browsing https://demos.captcha.com/demos/features/captcha-demo.aspx and finding the captcah image by c_captchademo_samplecaptcha_CaptchaImage as id of the captcha image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM