VB.Net Captcha等待用户输入

时间:2015-01-11 20:30:37

标签: vb.net webbrowser-control captcha

我试图将Captcha图像复制并显示在图片框中。

我目前正在使用以下代码查找相关ID,因为刷新页面时它会不断变化:

           Dim htmlDocument As HtmlDocument = Me.IEMC.Document
            Dim htmlElementCollection As HtmlElementCollection = htmlDocument.Images
            For Each htmlElement As HtmlElement In htmlElementCollection
                Dim imgUrl As String = htmlElement.GetAttribute("id")
                If imgUrl.Contains("wlspispHIPBimg") Then

                    GRABMYDARNIMG!

                End If
            Next

那工作正常。我可以让它找到正确的ID。但是,我将如何复制此ID的图像内容,并将其粘贴到我的图片框中?

我尝试过以下方法:

                IEMC.Document.GetElementById(imgUrl).Focus()
                IEMC.Document.ExecCommand("Copy", False, Nothing)

但似乎无法将其粘贴到任何地方,或者不确定如何正确复制元素。

我也尝试过这样的事情:

Dim O As Object

Set O = WebBrowser1.Document.body.createControlRange()
Call O.Add(WebBrowser1.Document.All("imgcode"))
Call O.execCommand("Copy")

Set Picture1.Picture = Clipboard.GetData

但这在VB.NET中不起作用。我不确定下一步该尝试什么。

假设将图像复制到剪贴板,将图像粘贴到图片框似乎很容易。我只是使用这行代码:

PictureBox1.Image = Clipboard.GetImage

如果有人知道如何做到这一点,我将非常感激。

注意: 无法从网址下载图片。它必须来自webbrowser中已加载的元素。一旦加载验证码图像,图像URL就会被删除。

编辑:尝试下面的建议..但是不能让它保存任何img。

Dim doc As mshtml.IHTMLDocument = DirectCast(IEMC.Document.DomDocument, mshtml.IHTMLDocument)
                    Dim imgRange As mshtml.IHTMLControlRange = DirectCast(DirectCast(doc.body, mshtml.HTMLBody).createControlRange(), mshtml.IHTMLControlRange)
                    Dim imagename As String = String.Empty
                    Try
                        For Each img As mshtml.IHTMLImgElement In doc.images
                            imgRange.add(DirectCast(img, mshtml.IHTMLControlElement))
                            imgRange.Select()
                            imgRange.execCommand("Copy", False, Nothing)
                            Using bmp As Bitmap = DirectCast(Clipboard.GetDataObject().GetData(DataFormats.Bitmap), Bitmap)
                                bmp.Save("C:\captchaPic.jpg")
                            End Using
                            imagename = img.nameProp

                            Exit For
                        Next
                    Catch aaa As System.Exception
                    End Try

2 个答案:

答案 0 :(得分:1)

我做了类似的事情,将CaptchaImage保存到磁盘,然后将其发送到decaptcha api。这是我在c#中的代码,但您可以轻松转换: 所以保存到磁盘并使用它放到图片框;

     IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
     IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
               string imagename = string.Empty;
                 try
                   {
                    foreach (IHTMLImgElement img in doc.images)
                     {
                       imgRange.add((IHTMLControlElement)img);
                       imgRange.execCommand("Copy", false, null);
                       using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
                                {
                                    bmp.Save(@"captchaimages\captchaPic.jpg");
                                }
                                imagename = img.nameProp;
                                break;

                            }
                        }
                        catch (System.Exception aaa)
                        { 
                        }

答案 1 :(得分:-1)

For Each capimg As HtmlElement In WebBrowser6.Document.Images
    If capimg.GetAttribute("src").Contains("papi/media?") Then
        Dim CD As mshtml.IHTMLDocument2 = CType(WebBrowser6.Document.DomDocument, Global.mshtml.IHTMLDocument2)
        Dim Images As mshtml.IHTMLControlRange = CType(CType(CD.body, mshtml.HTMLBody).createControlRange, Global.mshtml.IHTMLControlRange)
        For Each imgs As mshtml.IHTMLImgElement In CD.images
            If imgs.GetAttribute("src").ToString.Contains("papi/media?") Then
                Images.add(CType(imgs, Global.mshtml.IHTMLControlElement))
                Images.execCommand("Copy", False, Nothing)
                Dim captchaimage As Image
                captchaimage = CType(Clipboard.GetDataObject.GetData(DataFormats.Bitmap), Image)
                PictureBox1.Image = captchaimage

            End If
        Next
    End If
Next