Internet Explorer自动化访问图像

时间:2012-05-27 10:55:28

标签: c# internet-explorer

您好我有#IE自动化脚本,我正在尝试从HTMLImgClass中检索图像,我无法从缓存中获取图像,因为它没有保存,我不能再向src重新发送请求,因为新图像返回,所以我需要一种方法来访问浏览器内存中的图像。

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

使用上面的设置检索对象,这可以正常工作,但我不知道可以使用哪种方法来获取图像。

Thanx你的时间

解决 对于其他感兴趣的人,我这样解决了,我决定将图像复制到剪贴板,然后将其另存为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");
                                    }

1 个答案:

答案 0 :(得分:0)

我假设这与your other question有关。正如my answer there中所建议的那样,如果图像尚未保存到磁盘缓存中,您将无法在那里访问它。

解决方案是强制您的浏览器浏览所有请求的代理,从而在首先请求验证验证码图像。我过去曾使用过Fiddler2

在Fiddler2中,您可以更改OnBeforeResponse挂钩以按以下代码保存图像:

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

现在,只要您的程序稍后可以找到此脚本保存的文件(通常通过键入URL中的内容),您就可以开展业务了。