Internet Explorer 中的 vba 自动化 - 单击连续网页上的不同按钮不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8826097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:54:31  来源:igfitidea点击:

vba automation in Internet Explorer - clicking different buttons on successive webpages not working?

internet-explorervbabuttonautomationclick

提问by Gaffi

I am getting an error of "object variable or with block variable not set" when this macro runs. The highlighted line to debug is the 2nd one from the bottom - "estimate.Click". When I hover the mouse over "Set estimate" on the next line up, it says "estimate=Nothing". "estimate.submit" acts the same way. The corresponding button on the webpage is never clicked. All the rest of this code is working well.

当这个宏运行时,我收到“对象变量或块变量未设置”的错误。要调试的突出显示的行是从底部算起的第二行 - “estimate.Click”。当我将鼠标悬停在下一行的“设置估算”上时,它会显示“估算=无”。“estimate.submit”的行为方式相同。网页上的相应按钮永远不会被点击。此代码的所有其余部分都运行良好。

Sub btn_version()

Dim ieApp As Object
Dim ieDoc As Object
Dim ieForm As Object
Dim ieObj As Object
Dim URL As String
Dim estimate As Object

URL = "http://www.craft-e-corner.com/p-2688-new-testament-cricut-cartridge.aspx"
Set ieApp = CreateObject("InternetExplorer.Application")
ieApp.Visible = True
ieApp.navigate URL
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend

Set ieDoc = ieApp.document
Set ieForm = ieDoc.forms(1)
For Each ieObj In ieForm.Elements
If ieObj.ClassName = "AddToCartButton" Then
ieObj.Click
End If
Next ieObj

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
While ieApp.Busy Or ieApp.readyState <> 4: DoEvents: Wend
Set estimate = ieApp.document.getElementById("btnRequestEstimates")
estimate.Click
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub

回答by Gaffi

I'm not sure if this completely relates, but I had similar issues with JS errors using VBA automation. What I found to be successful was to make sure each and every JS function that would have been called by performing this action manually is still triggered using the code.

我不确定这是否完全相关,但我在使用 VBA 自动化时遇到了类似的 JS 错误问题。我发现成功的是确保通过手动执行此操作会调用的每个 JS 函数仍然使用代码触发。

For example, given this button:

例如,给定这个按钮:

<BUTTON id=btnExample onfocus="return focusFunction(this);" onclick="return clickFunction(this);" name=btnExample>

VBA to call:

要调用的 VBA:

    Set objButton = IE.Document.getElementByID("btnExample")
    objButton.onfocus
    objButton.onclick

There may also be other functions on the page or attached to other elements that need to be called as well (which may include some sort of page unload function), so you'll have to look into the code of that window to tell for sure.

页面上可能还有其他功能或附加到其他需要调用的元素(可能包括某种页面卸载功能),因此您必须查看该窗口的代码才能确定.

After I discovered this, following these steps has always been successful for me.

在我发现这一点后,按照这些步骤对我来说总是成功的。