利用 InternetExplorer.Application COM 物件啟動 IE 實例,導航至登入頁,等待載入完成 (readyState=4),透過 DOM 定位表單元素填入帳密並送出。
On Error Resume Next
url = "https://www.bathome.net/logging.php?action=login"
username = "***"
password = "***"
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate url, 4
Dim waittime : waittime = 0
Do Until 4 = ie.readyState
WScript.Sleep 200
waittime = waittime + 200
If waittime > 15000 Then Exit Do
Loop
If 4 <> ie.readyState Then
ie.Quit
WScript.Quit
End If
Set dom = ie.Document
Set form = dom.getElementById("loginform")
form.all("username").Value = username
form.all("password").Value = password
form.all("cookietime").Checked = True
form.all("loginsubmit").Click()
| 步驟 | 代碼 | 說明 |
|---|---|---|
| 建立 IE 實例 | CreateObject("InternetExplorer.Application") | 需安裝 IE 元件 (Win10/11 仍內建) |
| 顯示視窗 | ie.Visible = True | 除錯時可見;生產可設 False 隱藏 |
| 導航並等待 | Do Until 4 = ie.readyState | 4 = READYSTATE_COMPLETE |
| 超時保護 | waittime > 15000 | 避免網路卡住無限等待 |
| 取得 Document | ie.Document | 返回 HTMLDocument 介面 |
| 定位表單 | getElementById("loginform") | 依頁面實際 ID 調整 |
| 填寫欄位 | form.all("name").Value = ... | all 集合支援名稱/索引存取 |
| 勾選記住我 | .Checked = True | Checkbox/Radio 專用屬性 |
| 提交 | .Click() | 觸發按鈕點擊事件 |
InternetExplorer.Application COM 自動化。自動化登入腳本包含明文帳密,嚴禁用於生產/他人帳號。僅供學習 COM 自動化技術、自家測試環境使用。實際專案請採用 OAuth/SSO/API Token 等標準認證流程。
VBScript 驅動 IE 表單自動填寫是經典的「瀏覽器自動化 1.0」技術。雖然已不適用於現代 Web 環境,但其COM 互操作、DOM 操作、非同步等待的核心概念,在理解現代 WebDriver 架構時仍具參考價值。