iT邦幫忙

0

VBS AppActivate抓不到IE的視窗,登入鍵沒有給ID

想請問各位高手,小弟用VBS弄了一個登入網頁,目前遇到了兩個問題,請教各位高手哪裡有問題,網頁屬於內網,沒辦法提供連線測試。
1.AppActivate抓不到IE的視窗,以至於後面的SendKeys無法輸入。
2.此網頁登入鍵button沒有給ID,要怎麼給指令?

1.AppActivate抓不到IE的視窗,以至於後面的SendKeys無法輸入。
Dim wsh,ie
Set wsh = CreateObject("wscript.shell")
Set ie = WScript.CreateObject("InternetExplorer.Application")
URL=" http://......"
ie.Visible = True
ie.navigate URL
wsh.AppActivate "????? - Internet Explorer" '引號中填標題
WScript.Sleep 10001
wsh.SendKeys "?????" '引號中填帳號
WScript.Sleep 1000
1
wsh.SendKeys "{TAB}"
WScript.Sleep 10001
wsh.SendKeys "?????" '引號中填密碼
WScript.Sleep 1000
1
wsh.SendKeys "{ENTER}"

2.此網頁登入鍵button沒有給ID,要怎麼給指令?
< button type="submit" class="button"> 登入/button >
VBS如下
username="?????" '用?名
password="?????" '密?
Set ie = CreateObject("internetexplorer.application")
ie.navigate "http:/....."
ie.Visible = true
do While ie.busy Or ie.readystate <> 4
loop
ie.document.getElementById("account").value=username
ie.document.getElementById("password").value=password
ie.document.getElementByid("button").click

網頁原始碼From如下
< form class="form-horizontal" role="form" method="POST" action="http:....">
< input type="hidden" name="_token"value="woMDt8OLwgrpRvVk95Wjs15MdHXa9MOTMeH4u1Rb">

< label for="account" class="label">帳號< /label>
< input id="account" type="text" class="input" name="account" value="" required="" autofocus="">

< div class="group">
< label for="password" class="label">密碼< /label>
< input id="password" type="password" class="input" name="password" required="">

< div class="group">
< label for="password" class="label">< /label>

< div class="group">
< button type="submit" class="button"> 登入< /button>

                 < /form >
marlin12 iT邦研究生 5 級 ‧ 2018-09-17 20:31:23 檢舉
請把含有登入鍵的form那段html放上來
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
marlin12
iT邦研究生 5 級 ‧ 2018-09-18 19:55:19
最佳解答

1.AppActivate抓不到IE的視窗,以至於後面的SendKeys無法輸入。

AppActivate抓不到IE的視窗,因為IE還未完全啟動。
IE Automation這個石器時代的工具,是非常遲頓和不可靠的。在對它下每個指令前,必需要確定它做完之前的工作。如果要對它裏面的網頁下指令,更要等待網頁備妥。
因此,你的程式要改為

Dim objIE, url
url = "https://minecraft.net/zh-hant/login/"

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate url
objIE.Visible = True

Const READYSTATE_COMPLETE = 4
Do While objIE.Busy Or objIE.ReadyState <> READYSTATE_COMPLETE
	Wscript.Sleep 100
Loop

Wscript.Sleep 2000

' -----------------------------------------
Dim objShell
Set objShell = CreateObject("wscript.shell")
objShell.AppActivate "Internet Explorer"

再說,用SendKeys對IE下指令,非常依賴網頁當時的狀態,如果不配合便會下錯指令。
VBscript應該直接對objIE下指令,才較為可靠。

2.此網頁登入鍵button沒有給ID,要怎麼給指令?

如果要選取一個沒有ID的button,就要選用它的其他特徵(例如:屬性、內容)。
但是,如果所選用的特徵,在網頁中不是唯一的,便會下錯指令。

Dim strLoginName, strLoginPassword
strLoginName = "abc1234@gmail.com"
strLoginPassword = "abcdefg"

objIE.document.getElementByID("account").innerText = strLoginName
objIE.document.getElementByID("password").innerText = strLoginPassword

Wscript.Sleep 500

' <button type="submit" class="button"> 登入</button>
Dim objButton, strType, strText
For Each objButton In objIE.document.getElementsByTagName("button")
    strType = objButton.getAttribute("type")
    strText = Trim(objButton.innerText)

    If (strType = "submit") And (strText = "登入") Then
        objButton.Click
        Exit For
    End If
Next

[注意]如果VBscript文件裏有中文字,文件要用UTF16 LE格式來存放,否則會出現亂碼。

看更多先前的回應...收起先前的回應...
jim80815 iT邦新手 5 級 ‧ 2018-09-25 11:45:32 檢舉

非常感謝 marlin12大大的解答,解答非常清楚完全正確。
小弟初學者獻上萬分感謝。
另外再請教大大另一個問題,如上述第二個問題,如果要判斷是否已經登入,若已登入則不執行程式碼。是否可以用IF判斷是否有ID("account")則執行程式碼?

marlin12 iT邦研究生 5 級 ‧ 2018-09-25 23:53:03 檢舉

如果你想用ID="account"這個element來判斷是否已經登入,就要確定[已登入]和[登入失敗]的頁面沒有element的ID="account",否則就會誤判。

Dim elemLoginName 
Set elemLoginName = objIE.document.getElementByID("account")

If elemLoginName Is Nothing Then
    Wscript.Echo "[error] 'login name' element NOT exist !"
Else
    Dim strLoginName, strLoginPassword
    strLoginName = "abc1234@gmail.com"
    strLoginPassword = "abcdefg"

    elemLoginName.innerText = strLoginName
    objIE.document.getElementByID("password").innerText = strLoginPassword

    Wscript.Sleep 500

    ' <button type="submit" class="button"> 登入</button>
    Dim objButton, strType, strText
    For Each objButton In objIE.document.getElementsByTagName("button")
        strType = objButton.getAttribute("type")
        strText = Trim(objButton.innerText)

        If (strType = "submit") And (strText = "登入") Then
            objButton.Click
            Exit For
        End If
    Next
End If
jim80815 iT邦新手 5 級 ‧ 2018-09-26 14:27:40 檢舉

marlin12大大 感謝您的回覆,
更改後,IF未登入時操作正常,IF登入時出現錯誤,
Set elemLoginName = objIE.document.getElementByID("account")
上述這行錯誤顯示需要物件:'document.getElementByID(...)'
似乎是沒抓到account就出現錯誤?
請問,若要判斷有沒有登入,有更好的方法嗎?

marlin12 iT邦研究生 5 級 ‧ 2018-09-26 20:17:19 檢舉

不好意思,我太大意了。
對的,如果ID="account"這個element不存在,getElementByID("account")是會產生錯誤訊息。
要處理這個問題,只需要在程式的第一行,放入以下的代碼:

On Error Resume Next

意思是[如果碰到錯誤,便跳到跑下一行]。

jim80815 iT邦新手 5 級 ‧ 2018-09-28 14:53:16 檢舉

marlin12 大大感謝您的回覆,又讓我學到不少東西,我的問題完全解決了,獻上萬分感謝。

marlin12 iT邦研究生 5 級 ‧ 2018-09-28 23:07:08 檢舉

不用客氣,大家都在不斷學習。

我要發表回答

立即登入回答