简体   繁体   中英

Internet explorer automation accessing image

Hi i have ac# IE automation script and i am trying to retrieve the image from a HTMLImgClass, i cant get the image from cache because its not saved and i cant just resend a request to the src because a new image returns, so i need a way to access the Image in the browsers memory.

captcha_image = (HTMLImgClass)GetElementByPosition("img", 0, ie1);

The object is retreived with the above assignement, which works fine, but i have no idea which of the methods available i can use to get the image.

Thanx for your time

SOLVED For others interested, i solved it this way, i decided to copy the image to clipboard and then save it as a bmp

captcha_image = (HTMLImgClass)GetElementByPosition("img", 0, ie1);
                                    IHTMLImgElement captcha_image1 = (IHTMLImgElement)captcha_image;
                                    IHTMLDocument2 doc = (IHTMLDocument2)wb1.Document;
                                    IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
                                    imgRange.add((IHTMLControlElement)captcha_image1);

                                    imgRange.execCommand("Copy", false, null);
                                    using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                                    {
                                        bmp.Save(@"C:\skt.bmp");
                                    }

I'm assuming that this is related to your other question . As suggested in my answer there , if the image hasn't been saved to disk cache, you won't be able to access it there.

A solution is to force your browser to go through a proxy for all requests, and therefore to store the captcha images as they are being requested in the first place. I've used Fiddler2 for this in the past.

In Fiddler2, you can change the OnBeforeResponse hook to save the image by code similar to the following:

    if (oSession.uriContains("captcha")){      // change as needed
        oSession.utilDecodeResponse();
        var filename = oSession.url;           // get an appropriate file name
        oSession.SaveResponseBody(filename);   // store the file
    }

Now as long as the file being saved by this script can later be found by your program (usually by keying off something in the URL), you're in business.

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