0

i'm getting captcha image from webpage and inserting it to excel cell, then manually proceding this captcha by writing it to inputbox in excel, then excel pass this captcha text back to opened IE window.

So every element of code works fine, but i'm getting one version of captcha image on opened IE window and diffrent one inserted to excel.

Why is that ? It looks like diffrent instance of IE is opened and diffrent instance of IE was used for inserting captcha image to 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

Diffrent approach with screen capturing

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
Tomasz
  • 1
  • 1
  • 2
  • the web server probably serves up a different captcha each time there is a request for it. once by page load and the second time by the `.Insert` command. .... you could verify that by putting the captcha url in the web browser address bar and doing a refresh several times – jsotola Aug 23 '17 at 02:35
  • Captcha url refreshed in new browser window changes picture text. So is there a way to write this code diffrently, that both requests to page are done from the same instance of IE ? – Tomasz Aug 24 '17 at 10:33
  • screen capture then crop it will be the only solution ? – Tomasz Aug 24 '17 at 12:02
  • depends on what you are using it for. here is a direct way to get web data .... may or may not work for you .... you would have to find the captcha url in the html, download it, then formulate a response. (if the web page is built by java scripts, then it will not work) https://stackoverflow.com/questions/45200247/web-scraping-across-multipages-without-even-knowing-the-last-page-number – jsotola Aug 25 '17 at 20:20
  • are you able to share the URL to the web page that you are trying to reach? – jsotola Aug 25 '17 at 20:21
  • i added url. As i sad code is working, so this code grab captcha url and captcha img. But img is diffrent on opened IE window. – Tomasz Aug 26 '17 at 02:27
  • I added another code with different approach. I'm doing here print screen then cropping image and pasting to range. It's ok not perfect but do the work. Not perfect is how image is pasted after cropping. It looks like paste location is calculated according to image size before cropping. – Tomasz Aug 26 '17 at 11:14
  • i just checked the website. i right clicked the captcha and opened in new tab (using chrome browser). the captcha in the new tab was a different picture of the **same** captcha.. you can just download the captcha image from the image link. the text will be the same. – jsotola Aug 26 '17 at 23:47
  • In new tab yes, but in new window not anymore. I'm telling you from the beginning that it looks like navigate to web address is done on diffrent instance of IE and .insert command is done on another instance of IE (new window). I dont know why is that, im not that good with vba excel, but anyway, i manage to do this with screenshot method. – Tomasz Aug 28 '17 at 03:02

0 Answers0