Google的GMAIL相信大家都知道,基本上人人擁有,它以簡約的風格和方便的操作贏得了許多專業人士的好評,這兩天沒事自己用IE自動化模型以及HTML DOM寫了一個自動化GMAIL的VBScript腳本。
必要條件:
- 因爲是VBS,所有電腦都支持,不需要安裝任何工具
- 需要有Gmail帳號和密碼
腳本如下:
'**********************定義變量**********************
Dim gmail_username ' GMAIL用戶名
Dim gmail_password ' GMAIL密碼
Dim mail_to ' 需要發送的郵箱 Dim mail_subject ' 需要發送的標題
Dim mail_content ' 需要發送的內容
'********************** 初始化 **********************
gmail_username=""
gmail_password=""
mail_to= "[email protected]"
mail_subject="test"
mail_content="hi,zzxxbb112,您好"
'********************** script **********************
On Error Resume Next '錯誤繼續執行 Set oIE=CreateObject("InternetExplorer.Application")
'創建對象
oIE.Visible = True '設置可見
oIE.Navigate "http://www.gmail.com" '跳轉URL While oIE.Busy: Wend '等待頁面刷新完畢 With oIe.Document
.GetElementById("Email").value=gmail_username
.GetElementById("Passwd").value=gmail_password
.GetElementsByName("signIn")(0).click End With '登錄GMAIL While oIE.Busy: Wend '等待頁面刷新完畢
WScript.Sleep 5000 '等待2秒 Set oWsh=CreateObject("wscript.shell")
oWsh.SendKeys "{enter}" Set oWsh=Nothing '安全框確定 oIE.Navigate "https://mail.google.com/mail/?ui=html&zy=e"
'跳轉URL 標準HTML版本 While oIE.Busy: Wend '等待頁面刷新完畢 For Each a In oIE.Document.GetElementsByTagName("A")
If a.innertext="撰寫郵件" Then
a.click
Exit For
End If
Next '點擊撰寫郵件 While oIE.Busy: Wend '等待頁面刷新完畢 With oIE.Document
.GetElementByID("to").value=mail_to
.GetElementsByName("subject")(0).value=mail_subject
.GetElementsByName("body")(0).innertext=mail_content
.GetElementsByName("nvp_bu_send")(0).click End With '寫MAIL WScript.Sleep 3000 While oIE.Busy: Wend '等待頁面刷新完畢 For Each a In oIE.Document.GetElementsByTagName("A")
If a.innertext="退出" Then
a.click
Exit For
End If
Next '點擊退出
oIE.Quit '關閉瀏覽器 Set oIE=Nothing '釋放資源
'********************** end **********************
|
使用方法:
- 修改初始化中的5個參數,具體說明看腳本中的註釋。
- 另存爲以上腳本爲 *.vbs文件,雙擊直接運行。
總結:
此腳本同樣適合學習自動化測試的朋友,IE自動化模型和HTML DOM對於WEB自動化測試是非常重要的兩種技術,感興趣的朋友也可以去W3C學習下。