脚本从Excel读取并与网站交互并将结果再次写入Excel

时间:2017-08-07 20:01:03

标签: excel autoit

我正在尝试自动完成从网站获取结果的重复性工作, 这个链接Drugs.com,检查两种药物之间的相互作用 我必须从Excel表格中取出两种药物的文本,然后在网站上输入它们以检查它们之间的相互作用。 以下是Excel表格的示例:

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

当我按下'检查互动'必须提取下一页的结果并返回树交互之一:

- 主要
-moderate
-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

1 个答案:

答案 0 :(得分:0)

问题出在_IEFormElementSetValue函数中。

可能,

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

无法找到任何对象。

对于调试,您可以在_IEFormElementSetValue:

之前插入此代码
ConsoleWrite("isObject:" & isObj($text_form1) & @CRLF)

更新:我发现了3个问题。 1)迭代结束后你没有返回搜索页面,无法找到输入对象; 2)您必须在每次迭代开始时获取Input和Button对象; 3)每个If ... esleif部分都必须有Exitloop。

试试这段代码:

#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