Excel VBAsearch网站的search框并点击链接/button

我正在使用Excel VBAlogin到网站,然后在search框中input.txt文件名,然后单击button/链接在结果第二页中search它。 由此产生的第三页有一个图标,你必须点击下载文件。 最后,我想input.txt文件中的信息到一个新的Excel电子表格。

我的getLogin函数工作,我成功login到网站。 通过使用F8,我可以在各个字段中看到input我的用户名和密码值的每行代码。 我在第二页的水平,我想在search框中使用文件名search,但在

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

在SearchFile函数中,当我使用F8单步执行时,我发现它一直跳过FileN(0).Value = fileName,并且不input文件名。 我点击search的链接也是一样的,

 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代码:

search框 –

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

要search的链接 –

 <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>