【问题标题】:VBA IE automation - controlling child/new windowVBA IE自动化 - 控制子/新窗口
【发布时间】:2016-08-23 08:37:19
【问题描述】:

我希望有人可以帮助我。我有一个公司网站,我正在尝试浏览,所以当我最终设法到达我需要去的地方时,我可以自动完成一些表格。

我在代码中的某个地方通过代码“点击”了一个链接,并打开了一个新窗口。我需要能够单击新窗口上的链接,然后将控制切换回父窗口。

我已经看到了一些这样做的方法,但它们对我不起作用,或者更有可能我没有将它们正确地插入到我的代码中。

我在我的代码中放置了 shell 方法,运行时没有抛出任何错误,但它没有做任何事情,我不确定控件是否被移动到子窗口。

有人可以帮忙吗?

    Sub Fill_FormLog()

Dim ie As InternetExplorer
Dim URL As String
Dim objElement As Object
Dim objButton As Object
Dim objLink As Object
Dim objLink2 As Object
Dim objShell As Object

'Logs into website
   Set ie = CreateObject("InternetExplorer.Application")
    With ie
    .Visible = True
    .Navigate URL:="***parentwindowURL***"
    Do Until .ReadyState = 4
        DoEvents
    Loop
    Set mytextfield1 = .Document.all.Item("txtUserName")
    mytextfield1.Value = "***username***"
    Set mytextfield2 = .Document.all.Item("txtPassword")
    mytextfield2.Value = "***password***"
    ie.Document.getElementById("Submit").Click
    While .Busy Or .ReadyState <> 4: DoEvents: Wend
    End With

 'Opens the a link
 With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 ie.Navigate "***URLstillinparent***", , self
 End With

 'Opens the Profile menu
 With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 ie.Navigate "***anotherURLinparent***", , ["left"]
 End With

 'Opens the Profile search menu
 With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 ie.Navigate "***anotherURLinparent***", , ["mainParent"]
 End With

 'Copies the ID# from the Excel worksheet and pastes it to search in site
 With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:02"))
 Set objElement = .Document.frames("mainParent").Document.frames("main1").Document.forms("AgentIdentificationNumberSearch").Document.getElementById("IDN")
 objElement.Value = Sheets("Appointments").Range("a2").Value
 Set objButton = .Document.frames("mainParent").Document.frames("main1").Document.forms("AgentIdentificationNumberSearch").Document.getElementById("Search")
 objButton.Click
 End With

 'Opens view profile summary via child window
 With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:02"))
 Set objLink = .Document.frames("mainParent").Document.forms("AgentProfileList").Document.getElementById("grdProfile_r_0").Document.getElementsByTagName("a")(1)
 objLink.Click
 End With

 'Should move control to child window
 Application.Wait (Now + TimeValue("0:00:05"))
 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).LocationURL
    my_title = objShell.Windows(x).Document.Title

    If my_url       Like "***URLofChild/NewWindow***" Then
    Set ie = objShell.Windows(x)
    Exit For
Else
End If
 Next

 'Should click a link on the child window
 With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:02"))
 Set objLink2 = .Document.forms("form1").Document.getElementsByClassName("topHead").Document.getElementById("topLinks").Document.getElementsByTagName("a")(1)
 objLink2.Click
 End With

'if I can get the section above to work, I need to return control to the parent window at this point


 End Sub

【问题讨论】:

    标签: vba internet-explorer browser-automation


    【解决方案1】:

    我在另一个网站上找到了一些用于控制子窗口的代码,并且能够继续,但我有另一个问题需要处理,这使得这变得不必要。至少我现在知道该怎么做了。对我有用的代码如下。

    代码必须在对象而不是模块中才能工作。

            Option Explicit
    
    Public WithEvents IE1 As InternetExplorer
    Public IE2 As InternetExplorer
    
    
    Private Sub Automation()
    
    Dim objElement As Object
    Dim objButton As Object
    Dim objLink As Object
    Dim objLink2 As Object
    Dim mytextfield1 As Object
    Dim mytextfield2 As Object
    
    Set IE1 = CreateObject("internetexplorer.application")
    Set IE2 = Nothing
    
    With IE1
    .navigate "***website url***"
    .Visible = True  'allows for viewing the web page
    While .Busy Or .readyState <> 4: DoEvents: Wend
        Set mytextfield1 = .document.all.Item("txtUserName")
        mytextfield1.Value = "***username***"
        Set mytextfield2 = .document.all.Item("txtPassword")
        mytextfield2.Value = "***password***"
        While .Busy Or .readyState <> 4: DoEvents: Wend
        IE1.document.getElementById("Submit").Click
    End With
    
        ' loop until the page finishes loading
    Do While IE1.Busy: Loop
    
     'Opens another link
     With IE1
     While .Busy Or .readyState <> 4: DoEvents: Wend
     IE1.navigate "***url***"
     End With
    
     'Opens the menu
     With IE1
     While .Busy Or .readyState <> 4: DoEvents: Wend
     IE1.navigate "***URL in frame***", ["left"]
     End With
    
     'Opens the Profile search menu
     With IE1
     While .Busy Or .readyState <> 4: DoEvents: Wend
     IE1.navigate "***url in another frame***", , ["mainParent"]
     End With
    
     'Copies the ID# from the Excel worksheet and pastes it to search in site to search
     With IE1
     While .Busy Or .readyState <> 4: DoEvents: Wend
     Application.Wait (Now + TimeValue("0:00:03"))
     Set objElement = .document.frames("mainParent").document.frames("main1").document.forms("AgentIdentificationNumberSearch").document.getElementById("IDN")
     objElement.Value = Sheets("Appointments").Range("a2").Value
     Set objButton = .document.frames("mainParent").document.frames("main1").document.forms("AgentIdentificationNumberSearch").document.getElementById("Search")
     objButton.Click
     End With
    
     'Clicks "View Profile Summary" and opens new window
     With IE1
     While .Busy Or .readyState <> 4: DoEvents: Wend
     Application.Wait (Now + TimeValue("0:00:03"))
     Set objLink = IE1.document.frames("mainParent").document.forms("AgentProfileList").document.getElementById("grdProfile_r_0").document.getElementsByTagName("a")(1)
     objLink.Click
     End With
    
     'Ensure new window has been created (if the window does not generate, this will go on forever)
     Do While IE2 Is Nothing: Loop
     Do While IE2.Busy: Loop
    
    'Click first link in the new window
     With IE2
     While .Busy Or .readyState <> 4: DoEvents: Wend
     Application.Wait (Now + TimeValue("0:00:03"))
     Set objLink2 = IE2.document.forms("form1").document.getElementsByTagName("a")(2)
     objLink2.Click
     End With
    
     Set IE2 = Nothing
    
    End Sub
    
    ________________________
    
    Private Sub IE1_NewWindow2(ppDisp As Object, Cancel As Boolean)
        Set IE2 = New InternetExplorer
        Set ppDisp = IE2.Application
        Debug.Print "NewWindow2"
     End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 2013-05-17
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多