javascript - 在 VBA 中从 Javascript 获取 XmlHttpRequest 的 ReadyState

标签 javascript ajax vba excel

我有一个 Excel 文件,用于保存我们在工作中发现的东西。

我们必须登录网站才能检查某些内容。通过 VBA,我可以打开一个 IE 窗口,我可以用它登录网站并填写表格(然后提交!)。

但是,我的问题出现了,因为表单是使用 Javascript 和 AJAX 提交的。因此,我无法获得输出的响应数据(这就是我开始整个努力的目的)。

我的问题是:有什么方法可以在 VBA 中循环,直到 Javascript XML 对象获取数据,还是我尝试完全疯了?

编辑:由于请求,我正在放置我当前拥有的代码。由于我们的隐私协议(protocol),我无法发布这两个标有[已删除]的项目。我知道,一旦加载了响应数据,它就会附加到 ID 为“resultsTable”的 div 中。但是,我尝试循环直到找到该 DOM 元素是行不通的。

编辑 #2 我终于得到了一些东西。我的原始代码中的问题是我不知道 ohtml.getElementById() 函数如何工作以及如果什么也没找到它会返回什么。下面等待数据集出现。然后它对数据执行某些操作(不幸的是,我无法发布)。谢谢大家!

代码

Private Sub IE_Autiomation()
    Dim i As Long, r As Range
    Dim IE As InternetExplorer, ohtml As HTMLDocument
    Dim ouser As HTMLInputElement, opass As HTMLInputElement, otext As HTMLTextAreaElement, otable As HTMLTable
    
    ' Create InternetExplorer Object
    
    Set r = Application.Union(ActiveSheet.ListObjects(1).Range, ActiveSheet.ListObjects(2).Range, ActiveSheet.ListObjects(3).Range, ActiveSheet.ListObjects(4).Range)
    Debug.Print r.Address

    Set r = Application.Intersect(r, ActiveSheet.Range("B:B,E:E,H:H")) 'Just Trust Me, Ok?
    
    Set IE = New InternetExplorer

 
    ' Send the form data To URL As POST binary request
    IE.Navigate "[Removed]"
    
    
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    
    Set ohtml = IE.document
    Set ouser = ohtml.getElementById("USER")
    Set opass = ohtml.getElementById("PASSWORD")
    ouser.Value = "[removed]"
    
    
    pw = InputBox("Password?")
    opass.Value = pw
    IE.Visible = True
    ouser.form.submit
    
    Do While IE.Busy
        Application.Wait DateAdd("s", 1, Now)
    Loop
    
    Set otext = ohtml.getElementsByName("vin").Item(0)
    For Each e In r
        If e.Offset(0, -1).Value = "" And Not e.Value = "" Then
            otext.Value = otext.Value & e.Value & ", "
        End If
    Next
        Debug.Print otext.Value
        otext.Value = Left(otext.Value, Len(otext.Value) - 2)
        Debug.Print otext.Value
    
    Debug.Print "So far, wafles."
    
    For Each j In ohtml.getElementsByTagName("input")
        If j.Value = "Submit" Then
            Set ouser = j
            Exit For
        End If
    Next
    ouser.Click 
    'Here is where I need to wait for the response to load into the HTML
    
    ' Wait while IE loading...
    
    ' Clean up

    Set otable = ohtml.getElementById("resultsTable")
    
    Do While otable Is Nothing
        Application.Wait DateAdd("s", 1, Now)
        Set otable = ohtml.getElementById("resultsTable")
    Loop
    
    'IE.Quit
    Set IE = Nothing
    Set objElement = Nothing
    Set ohtml = Nothing
    Set objCollection = Nothing
 
    Application.StatusBar = ""
End Sub

最佳答案

使用就绪等待循环。

您将需要 WinAPI sleep 函数:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For use in timer function

然后,在发送请求后,您需要执行类似的操作(例如,对于 IE 对象,如果您使用代码更新 Q,我可以根据您的需要进行修改)。

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "http://google.com"
Do While Not IE.readyState = 4
    Sleep 250
Loop

它的作用是休眠 1/4 秒,循环直到文档加载完毕。

关于javascript - 在 VBA 中从 Javascript 获取 XmlHttpRequest 的 ReadyState,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23616203/

相关文章:

javascript - javascript 是按引用还是按值处理对象?

jQuery Ajax 多次加载 URL,如何正确取消绑定(bind)/重新绑定(bind)?

javascript - ArticlesController#show 中的 ActiveRecord::RecordNotFound

excel - 使用相对引用将公式从 [1,X] 范围复制到 [Y,X] 范围

VBA根据动态第一行值隐藏和取消隐藏列

javascript - 从 Angular 2中的json数组获取元素

javascript - 在 for 循环中将 2 个输入字段的值与第 3 个输入字段相乘

javascript - appspot 上的 cookiechoices 不起作用

jquery - 如何使用Ajax调用MVC4传递两个字符串数组

excel - 使用(偏移和调整大小)而不是(相交),引发运行时错误 '1004' :Application-defined or object-defined error