AutoIt 操作已打开火狐浏览器 (无需重新加载 geckodriver.exe)
您可以使用 AutoIt 来操作已打开的火狐浏览器,而不重新加载 geckodriver.exe。下面是一个示例代码:
#include <Array.au3>
#include <WinAPI.au3>
#include <Constants.au3>
#include <MsgBoxConstants.au3>
; 根据窗口标题查找窗口句柄
Func FindWindowByTitle($sTitle)
Local $aWinList = WinList()
For $i = 1 To $aWinList[0][0]
If $aWinList[$i][0] <> "" And WinGetTitle($aWinList[$i][1]) = $sTitle Then
Return $aWinList[$i][1]
EndIf
Next
Return 0
EndFunc
; 根据窗口句柄查找子窗口句柄
Func FindChildWindowByHandle($hParent, $sClass)
Local $aWinList = WinList($hParent)
For $i = 1 To $aWinList[0][0]
If $aWinList[$i][0] <> "" And BitAND(WinGetState($aWinList[$i][1]), $GUI_HIDE) = 0 And StringInStr(WinGetClassList($aWinList[$i][1]), $sClass) Then
Return $aWinList[$i][1]
EndIf
Next
Return 0
EndFunc
; 操作火狐浏览器
Func OperateFirefox()
; 查找火狐浏览器窗口句柄
Local $hFirefox = FindWindowByTitle('Mozilla Firefox')
If $hFirefox = 0 Then
MsgBox($MB_OK, '错误', '未找到火狐浏览器窗口')
Exit
EndIf
; 查找火狐浏览器主窗口句柄
Local $hFirefoxMain = FindChildWindowByHandle($hFirefox, 'MozillaWindowClass')
If $hFirefoxMain = 0 Then
MsgBox($MB_OK, '错误', '未找到火狐浏览器主窗口')
Exit
EndIf
; 查找火狐浏览器地址栏窗口句柄
Local $hFirefoxAddressBar = FindChildWindowByHandle($hFirefoxMain, 'MozillaWindowClass;MozillaWindowClass;MozillaWindowClass;MozillaWindowClass;MozillaWindowClass;MozillaWindowClass;MozillaWindowClass;MozillaWindowClass')
If $hFirefoxAddressBar = 0 Then
MsgBox($MB_OK, '错误', '未找到火狐浏览器地址栏')
Exit
EndIf
; 模拟键盘输入
ControlFocus($hFirefoxAddressBar, '', '')
ControlSend($hFirefoxAddressBar, '', '', 'www.example.com{ENTER}')
EndFunc
; 执行操作
OperateFirefox()
请注意,这个示例代码假设火狐浏览器已经打开并且至少有一个标签页是可见的。如果您的环境有所不同,请根据实际情况进行修改。
原文地址: https://www.cveoy.top/t/topic/pXDS 著作权归作者所有。请勿转载和采集!