Jump to content

Recommended Posts

Posted

Hello All,

I'm trying to execute a script that was developed by a friend of mine some time ago. 

I'm using AutoIT v3

Resuming: Basically the Script open an Excel, take the cells value (words), open IE, navigate to a site, use the words that are in Excel, download some files, than merge these files later.

What is happenning: Well, still when it's downloading files, it gives me the below error, some times in the 2nd archive, some times in the 7th, randomly.

 

image.png.044c6b34b52d4af96f60f0f8774a12c6.png

 

I'm also sharing the script code I'm using, to try to understand why this is happenning.

 

OBS: I'm working in a place where the connection isn't the best and I saw people talking about lost of connection could give this kind of error, so please, help me in these :P

OBS²: There are some texts in portuguese but don't care !

Thanks for all.

 

Code below:

 

 

#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <Excel.au3>
#Include <File.au3>

HotKeySet("{END}", "Terminate")

; *****************************************************************************
; Abre e faz a leitura do excel com as palavras para consulta >>
; *****************************************************************************

Local $oExcel = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\lista_parametros.xlsm")
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeRead Example", "Error opening workbook '" & @ScriptDir & "\lista_parametros.xlsx'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
 EndIf

  Local $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"), 2)

; *****************************************************************************
; Abre e faz a leitura do excel com as palavras para consulta <<
; *****************************************************************************
Local $sFldr1 = @ScriptDir & "\arquivos"
If DirGetSize($sFldr1) <> -1 Then
        ; MsgBox($MB_SYSTEMMODAL, "", "Directory already exists!")
     else
        DirCreate($sFldr1)
    EndIf


local $total = UBound($aResult) - 1


; *****************************************************************************
; Loop para consulta no site  >>
; *****************************************************************************

 For $i = 1 To UBound($aResult) - 1

      Local $oIE = _IECreate("http://www.camara.leg.br/buscaProposicoesWeb/pesquisaAvancada")
      Sleep(1000)
      Local $oForm = _IEFormGetCollection($oIE)
          ConsoleWrite("Testing " & $aResult[$i] & @CRLF)
      Local $oForm = _IEFormGetCollection($oIE)
      $button = _IEGetObjByName($oIE, "qualquerpalavra")

      _IEFormElementSetValue($button,  $aResult[$i])

      $button = _IEGetObjByName($oIE, "emtramitacao")
      _IEAction($button, "focus")
      _IEFormElementSetValue($button, "1")
      ;_IEAction($button, "focus")
      ; _IEFormElementOptionSelect($button,"1",1,"byValue",1)

      $button = _IEGetObjByName($oIE, "pesquisar")
      _IEFormElementSetValue($button, "Pesquisar")
      _IEAction($button, "click")

      _IELoadWait($oIE)

      Sleep(2000)

      Local $sMyString = "CSV"
      Local $oLinks = _IELinkGetCollection($oIE)
      For $oLink In $oLinks
          Local $sLinkText = _IEPropertyGet($oLink, "innerText")
          If StringInStr($sLinkText, $sMyString) Then
              _IEAction($oLink, "click")
              ExitLoop
          EndIf
       Next

       _IELoadWait($oIE)

       Sleep(5000)


       $dest= @ScriptDir & "\arquivos\arquivo_base_" &  $i & ".csv"
        Local $hIE = WinGetHandle("[Class:IEFrame]")
        Local $hCtrl = ControlGetHandle($hIE, "", "[ClassNN:DirectUIHWND1]")
        ControlSend($hIE ,"",$hCtrl,"{TAB}")          ; Gives focus to Open Button
        Sleep(500)
        ControlSend($hIE ,"",$hCtrl,"{TAB}")          ; Gives focus to Save Button
        Sleep(500)
        ControlSend($hIE ,"",$hCtrl,"{DOWN}")          ; Gives focus to Save As Button
        Sleep(500)
        ControlSend($hIE ,"",$hCtrl,"{DOWN}")          ; Gives focus to Save As Button
        Sleep(500)
        ControlSend($hIE ,"",$hCtrl,"{enter}")        ; Submit whatever control has focus


         WinWait("Save As")
        ; activate Save As window
        WinActivate("Save As")
      ; path to save the file is passed as command line arugment
        ControlFocus("Save As","","[CLASS:Edit;INSTANCE:1]")
        Send( $dest)
        sleep(500)
        Send( "{enter}")
      ; Submit whatever control has focus


    ;  $sMyString="Editar a busca"
    ;  $oLinks = _IELinkGetCollection($oIE)
    ;  For $oLink In $oLinks
    ;     Local $sLinkText = _IEPropertyGet($oLink, "innerText")
    ;      If StringInStr($sLinkText, $sMyString) Then
    ;          _IEAction($oLink, "click")
    ;          ExitLoop
    ;      EndIf
    ;   Next

      _IEQuit($oIE)
      sleep(1000)
   Next
; *****************************************************************************
; Loop para consulta no site  <<
; *****************************************************************************


; *****************************************************************************
; Corrige arquivos baixados >>
; *****************************************************************************
 Local $aFileList = _FileListToArray(@ScriptDir & "\arquivos\", "*")
;_ArrayDisplay($aFileList, "$aFileList")
 for $i = 1 to $aFileList[0]

   $file_name = @ScriptDir & "\arquivos\arquivo_base_" &  $i & ".csv"

   _FileWriteToLine($file_name, _FileCountLines($file_name), "", 1)
   _FileWriteToLine($file_name, _FileCountLines($file_name), "", 1)
   _FileWriteToLine($file_name, _FileCountLines($file_name), "", 1)
   _FileWriteToLine($file_name, 1, "", 1)
   _FileWriteToLine($file_name, 1, "", 1)
   _FileWriteToLine($file_name, 1, "", 1)
   if $i <> 1 then
      _FileWriteToLine($file_name, 1, "", 1)
   EndIf
Next

Run(@ScriptDir & "\juntaCSV.bat", "", @SW_MAXIMIZE)

$oExcel.run("Converter_output")

$oExcel.run("atualiza_banco")

Sleep(2000)

_Excel_Close($oExcel)

MsgBox($MB_SYSTEMMODAL, "Title", "Processo finalizado com sucesso!", 10)

Func Terminate()
    Exit
EndFunc   ;==>Terminate
 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...