Excel VBA搜索网站的搜索框并单击链接/按钮

时间:2017-07-26 17:03:10

标签: excel vba excel-vba

我正在使用Excel VBA登录网站,然后在搜索框中键入.txt文件名,然后单击按钮/链接以在生成的第二页中搜索它。生成的第三页有一个图标,您必须单击该图标才能下载该文件。最后,我想将.txt文件中的信息输入到新的Excel电子表格中。

我的getLogin功能正常,我成功登录网站。单步执行F8我可以看到每行代码在相应字段中输入我的用户名和密码值。我在第二页面上,我想使用文件名在搜索框中搜索,但是在

If Not FileN Is Nothing And FileN.Length > 0 Then
        FileN(0).Value = fileName 

在SearchFile函数中,当我使用F8单步执行它时,我看到它一直跳过FileN(0).Value = fileName并且不输入文件名。我点击搜索的链接也是如此,

Set ElementCol = ie.document.getElementsByTagName("a")
    For Each l In ElementCol
        If l.href = "javascript:myFunction('/mailbox/jsp/MBIList.jsp')" Then
            l.Click

它不会输入If语句,因此不会单击它。

到目前为止我的代码:

Sub getComponents()

   Dim WebAddressIn As String
   Dim ie As Object

   WebAddressIn = "https://..."

   'get ie instance to work with that is logged in
   Set ie = getLogin(WebAddressIn, "usern", "pw")

    Do Until ie.readyState = 4
    Loop

     ie.Visible = True

     Dim fileName As String
     fileName = Format(Now(), "yyyyMMdd") & ".TXT"
     Set ie = searchFile(fileName, ie)

End Sub

Function searchFile(fileName As String, ie As Object)
Dim Doc As Object, lastRow As Long, tblTR As Object
    Dim UserString As String
    Dim FileN As Object ' MSHTML.IHTMLElement
    Dim ElementCol As Object ' MSHTML.IHTMLElementCollection
    Dim AllText As String

    Do While ie.Busy
    Loop

    Set FileN = ie.document.getElementsByName("MsgNamePattern")
    If Not FileN Is Nothing And FileN.Length > 0 Then
        FileN(0).Value = fileName
    End If

    Do While ie.Busy
    Loop

    Set ElementCol = ie.document.getElementsByTagName("a")
    For Each l In ElementCol
        If l.href = "javascript:myFunction('/mailbox/jsp/MBIList.jsp')" Then
            l.Click
    Exit For
        End If
    Next l

    Do While ie.Busy
    Loop

    Set searchFile = ie
    Set ie = Nothing

End Function

Function getLogin(WebAddressIn As String, UserNameIn As String, PasswordIn As String)
Dim Doc As Object, lastRow As Long, tblTR As Object
    Set ie = CreateObject("internetexplorer.application")
    ie.Visible = False
    Dim UserString As String
    Dim PWString As String
    ie.Navigate2 WebAddressIn
    ie.Visible = True

    Dim UserN As Object ' MSHTML.IHTMLElement
    Dim PW As Object ' MSHTML.IHTMLElement
    Dim ElementCol As Object ' MSHTML.IHTMLElementCollection
    Dim AllText As String

    Do While ie.Busy
    Loop

    ' enter username and password in textboxes
    Set UserN = ie.document.getElementsByName("userid")
    If Not UserN Is Nothing And UserN.Length > 0 Then
        ' fill in first element named "username", assumed to be the login name field
        UserN(0).Value = UserNameIn
    End If

    Set PW = ie.document.getElementsByName("password")
    ' password
    If Not PW Is Nothing And PW.Length > 0 Then
        ' fill in first element named "password", assumed to be the password field
        PW(0).Value = PasswordIn
    End If

    Do While ie.Busy
    Loop

    'Clicks the Sign in button
    Set ElementCol = ie.document.getElementsByName("submit")
    For Each btnInput In ElementCol
        If btnInput.Value = "*Sign In" Then
            btnInput.Click
            Exit For
        End If
    Next btnInput

    Do While ie.Busy
    Loop

     Set getLogin = ie
    Set ie = Nothing

End Function

这是第二个结果页面上的相关HTML代码:

搜索框 -

<input type="text" name="MsgNamePattern" size="20" onblur="validateMessageName(this)">

搜索链接 -

<td align="center" valign="center"> <a href="javascript:myFunction('/mailbox/jsp/List.jsp')" onmouseover="chgButton('Go','GoOver'); window.status=''; return true;" onmouseout="chgButton('Go','GoOff'); window.status=''; return true;" onmousedown="chgButton('Go','GoDown'); return true;"><img border="0" src="/mailbox/images/go_off.gif" vspace="7" name="Go" align="top"></a> 
                  </td>

0 个答案:

没有答案