2017-04-23 293 views
0

大家好,在我訪問& VBA小應用程序的功能打開一個網頁,填寫用戶名和密碼,然後登錄。VBA打開網頁,登錄並獲得打開的頁面

如何從剛剛登錄的打開的網頁中讀取文本字段和其他內容,而不是在具有相同鏈接的新網頁中打開?

這是我的代碼。

感謝所有

Private Sub myButton_Click() 
Dim IE As Object 
Dim IEDoc As HTMLDocument 

Set IE = CreateObject("InternetExplorer.application") 
Set IEDoc = IE.Document 
IE.Navigate "http://mylogin.com" 
Do While IE.Busy: DoEvents: Loop 
While IE.ReadyState <> READYSTATE_COMPLETE 
DoEvents 
Wend 

With IEDoc 
     .getElementsByName("userid")(0).Value = "user" 
     .getElementsByName("password")(0).Value = "password" 
     .getElementById("btn_login").Click 
End With 

'At this point, I would read a text field from the new opened page, after login, in the same browser (not from a new webpage with the same link) 

回答

1

您添加等待答案頁面上點擊按鈕,然後 您可以通過它的名稱來訪問文本框值

Private Sub myButton_Click() 
Dim IE As Object 
Dim IEDoc As HTMLDocument 

Set IE = CreateObject("InternetExplorer.application") 
Set IEDoc = IE.Document 
IE.Navigate "http://mylogin.com" 
Do While IE.Busy: DoEvents: Loop 
While IE.ReadyState <> READYSTATE_COMPLETE 
DoEvents 
Wend 

With IEDoc 
     .getElementsByName("userid")(0).Value = "user" 
     .getElementsByName("password")(0).Value = "password" 
     .getElementById("btn_login").Click 
End With 

'At this point, I would read a text field from the new opened page, after login, in the same browser (not from a new webpage with the same link) 
Do While IE.Busy: DoEvents: Loop 
While IE.ReadyState <> READYSTATE_COMPLETE 
    DoEvents 
Wend 
With IEDoc 
     msgbox .getElementsByName("thetextfield").Value 
End With