[零基礎][06] vb6寫楓之谷外掛 – BeanFun登入篇

相信對於WebBrowser稍微有些認識的讀者來說,用它來填寫網頁上的內容、點擊網頁上的元素並不是一件太難的事,但拿來應用在樂豆的網頁上就不同了,即使使用Google Chrome的檢查元素能夠得知帳號輸入框的ID是「t_AccountID」,一般直接針對ID尋找的寫法卻並不能行,原因在於它被包在一個框架(Frame)內。既然我們偉大的洋蔥大神又大方的開源了,就讓我們看看大神的Level和小弟相距多遠吧<(_ _)>。


On Error GoTo Er '如果出現錯誤則跳躍到Er標籤
If Len(txt_User.Text) = 0 Or Len(txt_Pass.Text) = 0 Or Len(txt_Check.Text) = 0 Then Exit Sub

With WebB.Document.Frames.ifmForm1.Document
.GetElementById("t_AccountID").value = txt_User.Text
.GetElementById("t_Password").value = txt_Pass.Text
.GetElementById("CodeTextBox").value = txt_Check.Text
.GetElementById("btn_login").Click
End With
txt_Check.Text = vbNullString

Do: DoEvents: Loop While WebB.Busy

If InStr(WebB.Document.Frames.ifmForm1.Document.GetElementById("DivMsgBoxContainer").innertext, "錯誤") Then
MsgBox "帳密或驗證碼錯誤", vbInformation, "ERROR"
WebB.Document.Frames.ifmForm1.Document.GetElementById("MsgBoxOkBtn").Click
Exit Sub
End If

Er:
WebB.Navigate "http://tw.new.beanfun.com/game_zone/default.aspx"
(只做了一行的註解…)
然後我們來看看所需要的元件:

  • txt_User:使用者輸入帳號的文字框
  • txt_Pass:使用者輸入密碼的文字框
  • txt_Check::使用者輸入驗證碼的文字框
至於該怎麼取得驗證碼的圖片?以下也是洋蔥大神的Code,記得加入一個PictureBox,並命名為CheckPic。

Private Sub GetImage()
On Error GoTo Er
Dim ctrlRange As Object
Set ctrlRange = WebB.Document.Frames.ifmForm1.Document.body.createControlRange()
ctrlRange.Add (WebB.Document.Frames.ifmForm1.Document.GetElementById("c_login_idpass_form_samplecaptcha_CaptchaImage"))
'ctrlRange.Add (WebB.Document.Frames.ifmForm1.Document.images(1))
ctrlRange.execCommand ("Copy")
Delay (1) '修正剪貼簿無法開啟問題
CheckPic.Picture = Clipboard.GetData
Clipboard.Clear
Er:
End Sub