[英]How is it possible to click on a button present in a newly opened tab in internet explorer using VBA?
這是我寫的代碼。
Option Explicit
Public Sub Press_Button()
Dim objIE As SHDocVw.InternetExplorer 'microsoft internet controls (shdocvw.dll)
Dim htmlDoc As MSHTML.HTMLDocument 'Microsoft HTML Object Library
Dim htmlInput As MSHTML.HTMLInputElement
Dim htmlColl As MSHTML.IHTMLElementCollection
Dim the_input_elements As MSHTML.IHTMLElementCollection
Dim input_element As MSHTML.HTMLInputElement
Dim IeDoc As MSHTML.HTMLDocument
Dim IeDoc2 As MSHTML.HTMLDocument
Dim input_element2 As MSHTML.HTMLInputElement
Dim the_input_elements2 As MSHTML.IHTMLElementCollection
Set objIE = New SHDocVw.InternetExplorer
With objIE
.Navigate "https://www.ndexsystems.com/fengine/fullservice/en/kerrfinancialsalogin.go?fromLogoff=true" ' Main page
.Visible = 1
Do While .readyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:02"))
'PART 1: set user name and password
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("INPUT")
Do While htmlDoc.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If htmlInput.Name = "textbox_password" Then
htmlInput.Value = "***"
Else
If htmlInput.Name = "textbox_id" Then
htmlInput.Value = "***"
End If
End If
Next htmlInput
'PART 2: click login
Set htmlDoc = .document
Set htmlColl = htmlDoc.getElementsByTagName("input")
Do While htmlDoc.readyState <> "complete": DoEvents: Loop
For Each htmlInput In htmlColl
If Trim(htmlInput.Type) = "submit" Then
htmlInput.Click
Exit For
End If
Next htmlInput
'PART 3: Clicks on portfolio management button
Do While .Busy: DoEvents: Loop
Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc = .document
Set the_input_elements = IeDoc.getElementsByClassName("big_button")
For Each input_element In the_input_elements
If input_element.href = "javascript:changePageToFrontdoor(false);" Then
input_element.Click
Exit For
End If
Next input_element
'PART 4: Clicks on the 'Advanced search' button
Do While .Busy: DoEvents: Loop
Do Until .readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set IeDoc2 = .document
Set the_input_elements2 = IeDoc2.getElementsByClassName("parent-item")
For Each input_element2 In the_input_elements2
If input_element2.href = "javascript:directToSearch()" Then
input_element2.Click
Exit For
End If
Next input_element2
End With
End Sub
第1,2和3部分完美運行。 當我運行這個宏時,它實際上是使用我的憑據登錄網站。 在第3部分中,它還點擊了名為“Porfolio management”的按鈕。 但是,通過單擊“項目組合管理”按鈕,將打開一個新選項卡,其中包含同一網站的另一個頁面。 在這個新打開的頁面上,有一個名為“高級搜索”的按鈕,我想點擊它。 這是按鈕的HTML代碼。
第4部分不使用此代碼。 它沒有給我任何錯誤,它只是沒有做任何事情。 我不知道我的錯誤在哪里,因為我用與第3部分完全相同的語法編寫了第4部分,並且它只是第3部分實際運行並給出了正確的結果(單擊按鈕)。
也許第3部分打開這個網站的新標簽的事實應該意味着我在第4步沒有做的額外步驟? 因為我不再使用相同的標簽了...
任何人都可以幫我找到錯誤嗎?
謝謝 :)
如果URL每次都不同,並且您不能使用該URL,那么您可以嘗試參考以下步驟。
(1)創建Shell應用程序的對象。
(2)嘗試計算IE窗口並循環通過它。
(3)之后,將頁面標題與所有選項卡匹配,找到所需的選項卡並將其分配給IE對象,然后您可以嘗試與該頁面進行交互。
例:
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'find the desired page
If my_title Like "Put something from your IE page title here" & "*" Then
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
您可以根據您的要求修改上述代碼。
有關更多信息,請參閱以下鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.