首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与活动/单个IE11会话进行交互

与活动/单个IE11会话进行交互
EN

Stack Overflow用户
提问于 2016-02-05 20:15:10
回答 1查看 228关注 0票数 0

所以我有一个循环,从一个网站导出数据。但是,对于每一种情况,它都会启动一个新会话并关闭。是否有一种方法可以导航和下载one IE11会话中的所有情况,然后关闭?下面是我现在的代码:

代码语言:javascript
复制
Public Sub Get_File()

    Dim sFiletype As String     'Fund type reference
    Dim sFilename As String     'File name (fund type + date of download), if "" then default
    Dim sFolder As String       'Folder name (fund type), if "" then default
    Dim bReplace As Boolean     'To replace the existing file or not
    Dim sURL As String          'The URL to the location to extract information
    Dim Cell, Rng As Range
    Dim Sheet As Worksheet

    'Initialize variables
    Set Rng = Range("I2:I15")
    Set Sheet = ActiveWorkbook.Sheets("Macro_Button")

    For Each Cell In Rng
        If Cell <> "" Then
        sFiletype = Cell.Value
        sFilename = sFiletype & "_" & Format(Date, "mmddyyyy")
        sFolder = Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:J15"), 2, False)
        bReplace = True
        sURL = "www.preqin.com"

        'Download using the desired approach, XMLHTTP / IE
            If Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:W15"), 15, False) = 1 Then
            Call Download_Use_IE(sURL, sFilename, sFolder, bReplace)
            Else
            Call Download_NoLogin_Use_IE(sURL, sFilename, sFolder, bReplace)
            End If

        Else
        Exit Sub
        End If
    Next

End Sub

Private Sub Download_Use_IE(ByRef sURL As String, _
                            Optional ByRef sFilename As String = "", _
                            Optional ByRef sFolder As String = "", _
                            Optional ByRef bReplace As Boolean = True)

    Dim oBrowser As InternetExplorer
    Dim hDoc As HTMLDocument
    Dim objInputs As Object
    Dim ele As Object

    On Error GoTo ErrorHandler

    'Create IE object
    Set oBrowser = New InternetExplorer
    oBrowser.Visible = True

    'Navigate to URL
    Call oBrowser.navigate(sURL)
    While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend

    'Skips log in step if already signed into website
    On Error GoTo LoggedIn

    'Enter username
    oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_user_email").Value = "XXX"
    oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_user_password").Value = "XXX"

    'Submit the sign in
    oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_btnLogin").Click
    'Wait for website to load
    While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend

LoggedIn:

'All PE
    oBrowser.navigate Range("H3").Value
    'Wait for website to load
    While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend
    'Set the htmldocument
    Set hDoc = oBrowser.document

    'Loop and click the download file button
    Set objInputs = oBrowser.document.getElementsbyTagName("input")
    For Each ele In objInputs
        If ele.Title Like "Download Data to Excel" Then
            ele.Click
        End If
    Next

    'Wait for dialogue box to load
    While oBrowser.Busy Or oBrowser.readyState > 3: DoEvents: Wend
    Application.Wait (Now + TimeValue("0:00:02"))

    'IE 9+ requires to confirm save
    Call Download(oBrowser, sFilename, sFolder, bReplace)

    'Close IE
    oBrowser.Quit

Exit Sub

ErrorHandler:
    'Resume
    Debug.Print "Sub Download_Use_IE() " & Err & ": " & Error(Err)
End Sub
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-05 20:56:57

修改download_IE过程以使用传递给它的浏览器:

代码语言:javascript
复制
Private Sub Download_Use_IE(oBrowser As InternetExplorer, _
                           ByRef sURL As String, _
                        Optional ByRef sFilename As String = "", _
                        Optional ByRef sFolder As String = "", _
                        Optional ByRef bReplace As Boolean = True)

    Dim hDoc As HTMLDocument
    Dim objInputs As Object
    Dim ele As Object

    On Error GoTo ErrorHandler

    'Create IE object

    oBrowser.Visible = True

    'Navigate to URL

    Call oBrowser.navigate(sURL)

    ......rest of code

    Call Download(oBrowser, sFilename, sFolder, bReplace)

    'Do not Close IE

 Exit Sub

 ErrorHandler:
     'Resume
     Debug.Print "Sub Download_Use_IE() " & Err & ": " & Error(Err)
 End Sub

然后修改您的过程以传递此对象:

代码语言:javascript
复制
Public Sub Get_File()

    'declare all variables plus:

     Dim oBrowser As InternetExplorer

     Set oBrowser = New InternetExplorer

     .....put additional code here.....

     If Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:W15"), 15, False) = 1 Then
         Call Download_Use_IE(oBrowser, sURL, sFilename, sFolder, bReplace)
        Else
        Call Download_NoLogin_Use_IE(oBrowser, sURL, sFilename, sFolder, bReplace)
        End If
      Else
         Exit Sub
      End If
    Next

     'Close IE
     oBrowser.Quit

 End Sub

对于另一个过程,您需要做同样的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35232743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档