[英]How to navigate to an already opened internet explorer window? (VBA)
因此我一直在網上搜索有關如何執行此操作的很多信息,但似乎找不到任何具體信息。 我已經看到了很多有關如何打開新的Internet Explorer窗口並導航到特定網站的示例。 但是,在我的特定情況下,我希望能夠簡單地導航到一個已經打開的Internet Explorer窗口(這將是Internet Explorer唯一打開的窗口/選項卡)。 這是因為每次打開網站時,我需要先登錄才能進行任何操作。 然后,理想情況下,我想將一些ID粘貼到搜索框中,然后按Enter鍵(我應該能夠通過在線搜索來了解如何執行此操作)。
到目前為止,這是我找到的內容,但是我有點迷茫,並且不確定如何將這部分代碼按自己的意願應用。
Sub ExplorerTest()
Const myPageTitle As String = "Wikipedia"
Const myPageURL As String = "http://en.wikipedia.org/wiki/Main_Page"
Const mySearchForm As String = "searchform"
Const mySearchInput As String = "searchInput"
Const mySearchTerm As String = "Document Object Model"
Const myButton As String = "Go"
Dim myIE As SHDocVw.InternetExplorer
With myIE.Document.forms(mySearchForm)
'enter search term in text field
.elements(mySearchInput).Value = mySearchTerm
'press button "Go"
.elements(myButton).Click
End With
End Sub
這會完成所有瀏覽器窗口,包括Internet Explorer和Windows Explorer
Window
是Internet Explorer的Window對象。
Set objShell = CreateObject("Shell.Application")
Set AllWindows = objShell.Windows
For Each window in AllWindows
msgbox window.location
Next
或者,如果您確定這是唯一的開放時間
Set x = GetObject(,"InternetExplorer.Application")
我嘗試檢查您的描述,發現您想要獲取已打開的IE窗口的對象,然后嘗試使其自動化。
您可以嘗試參考下面的示例,可能會對您有所幫助。
在此示例中,您可以看到使用其標題搜索已打開的頁面。 比起您可以使用它的對象來自動化該頁面。
Sub demo()
Dim str_val As Object
marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next
my_url = objShell.Windows(x).document.Location
my_title = objShell.Windows(x).document.Title
If my_title Like "XYZ" & "*" Then
Set IE = objShell.Windows(x)
marker = 1
Exit For
Else
End If
Next
If marker = 0 Then
MsgBox ("A matching webpage was NOT found")
Else
MsgBox ("A matching webpage was found")
Set str_val = IE.document.getElementById("txtbox1")
str_val.Value = "demo text"
End If
End Sub
輸出:
參考:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.