首页 > 解决方案 > AutoIT 脚本以某种方式未检测到网站上的下载按钮

问题描述

我正在尝试编写一个 AutoIT 脚本,该脚本将通过 Internet Explorer 自动导航到 URL(https://www.macrotrends.net/1476/copper-prices-historical-chart-data)并单击一个按钮(红色,标记为“下载历史数据”)将下载 Excel 数据文件。通过检查这个按钮,我发现它有一个 id(“dataDownload”),但是我在 AutoIT 中使用 _IEGetObjById 函数的尝试没有成功。这是我的脚本:

#include <IE.au3>

Local $oIE = _IECreate("https://www.macrotrends.net/1476/copper-prices-historical-chart-data", 1)
Local $oButton = _IEGetObjById($oIE, "dataDownload")
_IEAction($oButton, "click")

该脚本正在打开 Internet Explorer,但我认为它没有按预期检测按钮。我的怀疑是,这可能是因为按钮位于框架或容器或您可能称之为的任何内容中?

此外,我的意图是让这个 AutoIT 脚本由 Windows 上的批处理文件调用,并按计划运行。如果您有任何其他推荐的用于此类自动网络抓取的方法,请随时启发。

非常感谢我能得到的所有帮助。

标签: pythonweb-scrapingautoit

解决方案


这是使用https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/的工作示例 您可以忽略大多数全局变量。我将其用作我编写的另一个脚本的模板。

```
#include <GUIConstantsEx.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPIGdiDC.au3>
#include <WinAPIHObj.au3>
#include <WinAPISysWin.au3>
#include <AD.au3>
#include <Array.au3>
#include "UIAWrappers.au3"
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <GuiListBox.au3>
#include <Word.au3>
#include <File.au3>
#include <Excel.au3>


Opt("WinWaitDelay", 150)
Opt("WinTitleMatchMode", 2)
Opt("WinDetectHiddenText", 1)
Opt("MouseCoordMode", 2)
Opt("SendKeyDelay", 10)
Opt("GUIResizeMode", 1)
HotKeySet("^!x", "MyExit")
Global $ver = "1.0.2.7"
Global $__goError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc") ; At the top of your script
Global $oCOMErrorHandler = ObjEvent("AutoIt.Error", "_User_ErrFunc")
Global $Guih, $MAINTab, $MAIN, $button1, $button2, $button3, $button4, $idListBox1, $inputEmployeeUsername, $inputSAName, $ADusername, $iLastTab, $editOU
Global $inputSAPassword, $editEmail, $editEmployeeID, $editLastName, $editCity, $editRegion, $editTitle, $editManager, $ADpassword, $editState, $DF
Global $C9, $hWnd, $g_szVersion, $msg, $employeeName, $aResult, $aManager, $aProperties1, $aProperties2, $aString, $aString1, $aString2, $iCurrTab, $editFirstName
Global $ManagerUN, $OU, $Line_Split, $iIndex, $GulfCoast, $SouthEast, $MountainWest, $MidAtlantic, $SouthTexas, $NorthTexas, $MidWest, $MidEast, $editZip
Global $file1, $OU_Name_Return_R, $OU_Name_Trim_L, $OU_Name_Split, $OU_Name_Trim_R, $Search, $State, $TD, $hWnd2, $oWord1, $button11, $editStreet, $CSVName, $Web

$Web = "https://www.macrotrends.net/1476/copper-prices-historical-chart-data"

ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "--new-window --force-renderer-accessibility " & $Web, "", "")
WinWait("Copper Prices - 45 Year Historical Chart | MacroTrends - Google Chrome", "", 4)
$hWnd = WinGetHandle("Copper Prices - 45 Year Historical Chart | MacroTrends - Google Chrome")
WinActivate($hWnd)
WinSetState($hWnd, "", @SW_MAXIMIZE)

Local $oP8=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=Copper Prices - 45 Year Historical Chart | MacroTrends - Google Chrome;controltype:=UIA_PaneControlTypeId;class:=Chrome_WidgetWin_1", $treescope_children)
_UIA_Action($oP8,"setfocus")
Local $oP7=_UIA_getObjectByFindAll($oP8, "Title:=Copper Prices - 45 Year Historical Chart | MacroTrends;controltype:=UIA_DocumentControlTypeId;class:=Chrome_RenderWidgetHostHWND", $treescope_children)
_UIA_Action($oP7,"setfocus")

;~ First find the object in the parent before you can do something
Local $oUIElement=_UIA_getObjectByFindAll("  DownloadHistoricalData.mainwindow", "title:=  Download Historical Data;ControlType:=UIA_ButtonControlTypeId", $treescope_subtree)
Local $oUIElement=_UIA_getObjectByFindAll($oP7, "title:=  Download Historical Data;ControlType:=UIA_ButtonControlTypeId", $treescope_subtree)
_UIA_Action($oUIElement, "setfocus")
_UIA_Action($oUIElement, "highlight")
_UIA_Action($oUIElement, "activate")
_UIA_Action($oUIElement, "leftclick")

Func MyExit()
    Exit
EndFunc   ;==>MyExit

````

推荐阅读