如何使循环工作?

时间:2017-07-01 01:03:51

标签: vba

我花了几个小时阅读Stack Overflow和其他网站,但找不到适合我的答案。我正在使用VBA,并使用Microsoft Internet Controls和Microsoft HTML Object Library。

我得到第一个do循环工作,等待网站加载后输入用户和密码信息。

但我需要第二个do循环,因为我被重定向到第二个网页,我还必须在文本字段中输入信息。

但是 - 对于我的生活 - 我无法让第二个循环工作。

这是我的代码(我已经指出了粗体中不起作用的部分):

' Incorporating Microsoft Internet Controls AND Microsoft HTML Object Library
' Webpage loaded constant
Const READYSTATE_COMPLETE = 4
' Declare Windows API function for setting active window
Declare Function SetForegroundWindow Lib "user32" _
Alias "SetForegroundWindow" (ByVal Hwnd As Long)As Long
' Declare Internet Explorer object
Dim IE As SHDocVw.InternetExplorer
Sub Main
SetMicrophone 0
   ' create instance of InternetExplorer
   Set IE = New InternetExplorer
   ' using your newly created instance of Internet Explorer
   With IE
      SetForegroundWindow IE.HWND
      .Visible = True
      .Navigate2 "http://www.NakedCapitalism.com/wp-admin"
     ' Wait until page we are navigating to is loaded
      Do While .Busy
      Loop
      Do
      Loop Until .readyState = READYSTATE_COMPLETE
      ' the username and password values will not exist if already logged in
      ' so no need to fill in the values and go straight to Next
      On Error Resume Next
         If Err Then
           'Do Nothing
         Else
          ' When the page is fully loaded enter your username and password
          ' you will need to set these appropriately
  End If
     Dim inputs As MSHTML.IHTMLElementCollection
    Dim iFrames As MSHTML.IHTMLElementCollection
    Dim iFrame As MSHTML.HTMLFrameElement
    ' Get top_window frame and navigate to it then
    Set doc = IE.document
    Set iFrames = doc.getElementsByName("top_window")
    If Not iFrames Is Nothing Then
        Set iFrame = iFrames(0)
        IE.navigate url & iFrame.src
              Set inputs = doc.getElementsByName("log")
        If Not inputs Is Nothing Then
            inputs(0).value = "MyUserName"
        End If
    End If
    Set IE = Nothing
   End With
    ' Get top_window frame and navigate to it then
    Set doc = IE.document
    Set iFrames = doc.getElementsByName("top_window")
    If Not iFrames Is Nothing Then
        Set iFrame = iFrames(0)
        IE.navigate url & iFrame.src
        Set inputs = doc.getElementsByName("pwd")
        If Not inputs Is Nothing Then
            inputs(0).value = "MyPassword"
        End If
    End If

doc.getElementsByName( “WP-提交”)。第(0)。单击()

      **' Wait until fully logged in
      Do While .Busy
      Loop**
      ' I take another action here, which I can't execute until the web page
      ' redirects to the new web page

    Set IE = Nothing
   ' Tidy Up
   SetMicrophone 1
End Sub

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

也许第二次网页使用ajax或其他东西而不是网页导航来登录,所以你不能使用“Do While .Busy”,因为那里没有导航

答案 1 :(得分:0)

好的,我明白了。在第二个网页加载时,第二个do循环的代码等待如下:

Do While IE.Document.getElementById("Unique ID Which Only Appears On the Second Web Page") Is Nothing
Loop

感谢大家。

答案 2 :(得分:-1)

如果您想将数据输入到html表单并提交,请执行此操作。

Public Function getLoginResponse() as String

Dim result As String
Dim myURL As String, postData As String
Dim winHttpReq As Object
Dim strUsername as String, strPassword as String

strUsername = "YourUserName" ' Put your Username here
strPassword = "YourPassWord" ' Put Your Password here

Set winHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

myURL = "http://www.nakedcapitalism.com/wp-login.php"
postData = "log=" & strUsername & "&pwd=" & strPassword ' Set Username and Password to name of input in form

winHttpReq.Open "POST", myURL, False
winHttpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
winHttpReq.Send (postData)

result = winHttpReq.responseText

getLoginResponse = result

Set winHttpReq = Nothing

End Function

此函数返回您可以使用的html文档,如IE.document

如果您查看网站的html表单,请参阅

<form name="loginform" id="loginform" action="http://www.nakedcapitalism.com/wp-login.php" method="post" style="position: static; left: 0px;">

它显示了一个帖子表单,因此请在请求中使用post方法。

<input name="log" id="user_login" aria-describedby="login_error" 

这显示用户名的输入名称为log,因此我们将其用作postData字符串中的第一个参数

<input name="pwd" id="user_pass" aria-describedby="login_error" class="input" value="" size="20" type="password"> 

显示密码输入的名称是pwd,因此我们将其用作postData中的第二个参数。

代码被盗here