2017-08-07 48 views
0

我試圖自動執行一些重複性工作以從網站獲取結果, 此鏈接Drugs.com檢查兩種藥物之間的相互作用。
我必須從Excel工作表中取出兩種藥物的文本,並在網站上輸入它們來檢查它們之間的相互作用。 這裏是我的Excel工作表的樣本:腳本從Excel中讀取並與網站交互並將結果再次寫入Excel

column(A)   Column(B) 
(A1)candesartan  benazepril 
(A2)eprosartan  captopril 
(A3)irbesartan  enalapril 

當我按「確認互動」下一頁的結果必須被提取並返回樹相互作用之一:

- 主要
- 適度
-minor

那麼必須將結果寫入列(C)

我在AutoIt的初學者,但我可以這樣做我腳本雖然有很多錯誤。 如果有人能糾正/幫助我解決我代碼中的錯誤,我將不勝感激。如果有人可以用正確的關鍵字幫助我,我也會很感激,這樣我就可以通過Google獲得示例和解決方案。 謝謝大家。

#include <Excel.au3> 
#include <IE.au3> 
#include <File.au3> 

Local $sWorkbook = "C:\Users\Aligaaly\Desktop\autoit\test\drugs.xlsx" 
Local $oExcel = _Excel_Open() 
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook) 
$oWorkbook = _Excel_BookAttach($sWorkbook) 


GLOBAL $oIE = _IECreate("https://www.drugs.com/drug_interactions.php") 
Local $oInputs = _IETagNameGetCollection($oIE, "input") 

For $oInput In $oInputs 
     $text_form1 = _IEGetObjById($oIE, "livesearch-interaction") 
     If StringLower($oInput.classname) == "input-button search-button" and _IEFormElementGetValue($oInput) Then ; it is an input 
        Global $oInput_btn = $oInput 
     EndIf 
Next 


WinActivate("[CLASS:XLMAIN]", "") 

For $i = 1 To 5 
Global $sResulta = _Excel_RangeRead($oWorkbook, Default, 'A' & $i & ':A' & $i,1) 

     For $y = 1 To 5 
     Global $sResultb = _Excel_RangeRead($oWorkbook, Default, 'B' & $y & ':B' & $y,1) 

       WinActivate("[CLASS:IEFrame]", "") 
       _IEFormElementSetValue($text_form1, $sResulta) 
       _IEAction ($oInput_btn, "click") 
      sleep(5000) 
       _IEFormElementSetValue($text_form1, $sResultb) 
       _IEAction ($oInput_btn, "click") 
      sleep(5000) 


      For $oInput In $oInputs 
        If StringLower($oInput.value) == "check for interactions" Then 
         Global $check_btn = $oInput 
        EndIf 
      Next 
      _IEAction ($check_btn, "click") 


      sleep(5000) 

      $oButtonsa = _IETagnameGetCollection($oIE, "span") 
      For $oButtonn in $oButtonsa 
       If $oButtonn.classname == "status-category status-category-major" Then 
         WinActivate("[CLASS:XLMAIN]", "") 
         _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "major","C" & $y) 
       ElseIf $oButtonn.classname == "status-category status-category-moderate" Then 
         WinActivate("[CLASS:XLMAIN]", "") 
         _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "moderate","C" & $y) 
       ElseIf $oButtonn.classname == "status-category status-category-minor" Then 
         WinActivate("[CLASS:XLMAIN]", "") 
         _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "minor","C" & $y) 
       EndIf 
       ExitLoop 

      Next 
     Next 
Next 

我和我最後的潤色更新代碼 我覺得這個代碼已經完成我必須寫下上面 步驟,但我有一個錯誤,當劇本完成第一次迭代 enter image description here

+0

是什麼結果頁面的網址是什麼樣子?您完全可以直接提交網址並獲得結果,然後從該網頁中刪除結果。這意味着你可以通過直接提交和抓取數據,而不是搜索字段和按鈕和東西來做到這一點 –

+0

不幸的是,該鏈接有ID的我不知道它是什麼指示結果鏈接樣本 https://www.drugs。 com/interactions-check.php?drug_list = 917-482,11-2692 –

+0

在這種情況下,可能會堅持使用Autoit。我實際上在腳本中看不到錯誤消息中的代碼。 –

回答

0

問題出在_IEFormElementSetValue函數中。

也許,

$text_form1 = _IEGetObjById($oIE, "livesearch-interaction") 

找不到任何對象。

調試它,你可以_IEFormElementSetValue之前插入此代碼:

ConsoleWrite("isObject:" & isObj($text_form1) & @CRLF) 

更新:我發現3個問題。 1)迭代結束後沒有返回到搜索頁面,輸入對象無法找到; 2)您必須在每次迭代開始時獲得Input和Button對象; 3)Exitloop必須位於每個If ... esleif部分。

試試這個代碼:

#include <Excel.au3> 
#include <IE.au3> 
#include <File.au3> 

Local $sWorkbook = "C:\Users\Aligaaly\Desktop\autoit\test\drugs.xlsx" 
Local $oExcel = _Excel_Open() 
Local $oWorkbook = _Excel_BookOpen($oExcel, $sWorkbook) 
$oWorkbook = _Excel_BookAttach($sWorkbook) 


GLOBAL $oIE = _IECreate("https://www.drugs.com/drug_interactions.php") 
Local $oInputs = _IETagNameGetCollection($oIE, "input") 

For $oInput In $oInputs 
     $text_form1 = _IEGetObjById($oIE, "livesearch-interaction") 
     If StringLower($oInput.classname) == "input-button search-button" and _IEFormElementGetValue($oInput) Then ; it is an input 
        Global $oInput_btn = $oInput 
     EndIf 
Next 


WinActivate("[CLASS:XLMAIN]", "") 

For $i = 1 To 5 
    Global $sResulta = _Excel_RangeRead($oWorkbook, Default, 'A' & $i & ':A' & $i,1) 

      For $y = 1 To 5 
      Global $sResultb = _Excel_RangeRead($oWorkbook, Default, 'B' & $y & ':B' & $y,1) 

       $oInputs = _IETagNameGetCollection($oIE, "input") 

       For $oInput In $oInputs 
         $text_form1 = _IEGetObjById($oIE, "livesearch-interaction") 
         If StringLower($oInput.classname) == "input-button search-button" and _IEFormElementGetValue($oInput) Then ; it is an input 
          $oInput_btn = $oInput 
         EndIf 
       Next 

        WinActivate("[CLASS:IEFrame]", "") 
        _IEFormElementSetValue($text_form1, $sResulta) 
        _IEAction ($oInput_btn, "click") 
       sleep(5000) 
        _IEFormElementSetValue($text_form1, $sResultb) 
        _IEAction ($oInput_btn, "click") 
       sleep(5000) 


       For $oInput In $oInputs 
         If StringLower($oInput.value) == "check for interactions" Then 
          Global $check_btn = $oInput 
         EndIf 
       Next 
       _IEAction ($check_btn, "click") 


       sleep(5000) 

       $oButtonsa = _IETagnameGetCollection($oIE, "span") 
       For $oButtonn in $oButtonsa 
        If $oButtonn.classname == "status-category status-category-major" Then 
          WinActivate("[CLASS:XLMAIN]", "") 
          _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "major","C" & $y) 
          ExitLoop 
        ElseIf $oButtonn.classname == "status-category status-category-moderate" Then 
          WinActivate("[CLASS:XLMAIN]", "") 
          _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "moderate","C" & $y) 
          ExitLoop 
        ElseIf $oButtonn.classname == "status-category status-category-minor" Then 
          WinActivate("[CLASS:XLMAIN]", "") 
          _Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, "minor","C" & $y) 
          ExitLoop 
        EndIf  

       Next 
       _IENavigate($oIE, "https://www.drugs.com/drug_interactions.php?action=new_list") 
      Next 
    Next 
相關問題