Excel VBA获取验证码 - 图像的错误版本

时间:2017-08-23 01:39:43

标签: excel vba captcha

我从网页获取验证码图像并将其插入excel单元格,然后通过将此验证码写入excel中的输入框来手动处理此验证码,然后excel将此验证码文本传递回打开的IE窗口。

因此,代码的每个元素都可以正常工作,但我在打开的IE窗口中获取了一个版本的验证码图像,并且将不同的代码插入到excel中。

为什么?看起来IE的不同实例被打开,IE的不同实例用于将验证码图像插入excel。

Sub Get_captcha()
Dim ie As New InternetExplorer

ie.Visible = True
ie.Navigate ActiveCell

Do
DoEvents
Loop Until (ie.ReadyState = 4 And Not ie.Busy)

Dim doc As HTMLDocument
Set doc = ie.Document
Dim MyInput As String

captcha = doc.getElementById("MainContent_ctrlCaptcha").getAttribute("src")
ActiveCell.Offset(0, 2).Value = captcha

With ActiveSheet.Pictures
 .Insert (doc.getElementById("MainContent_ctrlCaptcha").getAttribute("src"))
End With
End Sub

屏幕捕获的不同方法

Sub Get_captcha()
 Application.DisplayAlerts = False: Application.ScreenUpdating = False: Application.EnableEvents = False
    On Error GoTo Cleanup

Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.Clear

Dim ie As New InternetExplorer
ie.Visible = True
ie.Navigate ActiveCell.Value

Do
DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE

Dim doc As HTMLDocument
Set doc = ie.Document
Dim MyInput As String

captcha = doc.getElementById("MainContent_ctrlCaptcha").getAttribute("src")
ActiveCell.Offset(0, 2).Value = captcha

Sleep (500)
Application.SendKeys "(%{1068})"
Sleep (500)
ie.Quit

Dim shp As Shape

ActiveSheet.Paste Destination:=ActiveSheet.Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(-12, -8)), link:=False
With ActiveSheet
Set shp = .Shapes(.Shapes.Count)
    shp.PictureFormat.CropRight = 500
    shp.PictureFormat.CropBottom = 330
    shp.PictureFormat.CropTop = 150
    shp.PictureFormat.CropLeft = 278
End With

ActiveCell.Offset(1).Select

Cleanup:
    Application.DisplayAlerts = True: Application.ScreenUpdating = True: Application.EnableEvents = True
End Sub

0 个答案:

没有答案