使用VBA时,Form.Submit无法提交。

5
我有一个网页,需要从中提取数据。我可以使用VBA做到除了点击图像元素外的所有操作,而点击图像元素会提交表单并创建一个带有数据的弹出窗口。
其中,图片元素中的一个属性名为“productguid”,其字符串值为“a12-545”。
在我用鼠标点击图像元素之前,表单的HTML代码如下。
<form id="GetProductQuantitiesForAccessibleBranches" action="GetProductQuantitiesForAccessibleBranches" method="post">
  <input type="hidden" name="ProductGuid" value="">
</form>

这是我用鼠标手动点击后的HTML代码:

<form id="GetProductQuantitiesForAccessibleBranches" action="GetProductQuantitiesForAccessibleBranches" method="post">
  <input type="hidden" name="ProductGuid" value="a12-545">
</form>

基于此,我假设在提交表单之前,图像元素中的productguid值将通过传递到表单。下面是我的VBA代码:

'Change the input element value
ie.Document.getElementById("GetProductQuantitiesForAccessibleBranches").all.item(0).value = "a12-545"

'Submit the form
ie.Document.getElementyId("GetProductQuantitiesForAccessibleBranches").Submit

根据“本地窗口”,所有Javascript事件均为“Null”。这两行代码都可以正常运行,但网页没有更新。我是否遗漏了什么?
我也尝试使用“.Click”方法仅单击图像元素,但那也没有任何作用。
该网页受密码保护,因此我无法公开发布URL。
更新:以下是通常手动点击以提交表单的标记中的HTML。也许我可以从中找到一些有用的内容?
<img alt="View Quantities At Other Locations" src="/WebOrder/Images/CheckQtys.gif" 
title="View Quantities At Other Locations" class="popup" 
popupdirection="upperleft" popupwidth="380" 
popupcontent="#ProductQuantitiesForAccessibleBranches" 
onbeforepopupcreate="onBeforePopupCreate_GetProductQuantitiesForAccessibleBranches(this)" 
popupajaxformid="GetProductQuantitiesForAccessibleBranches" 
onbeforepopupajaxpost="onBeforePopupAjaxPost_GetProductQuantitiesForAccessibleBranches(this)" 
oncompletepopupajaxpost="onCompletePopupAjaxPost_GetProductQuantitiesForAccessibleBranches(this)" 
productguid="a12-545" 
displayitem="33902" 
brandguid="00000000-0000-0000-0000-000000000000" 
brandname="" brandsku="">

如果页面元素的值是基于点击事件而改变的,我敢打赌所有的JavaScript事件都不为空。IE对象可能由于某些原因没有显示一些事件,但深入研究JavaScript肯定是理解发生了什么的关键。使用您最喜欢的浏览器的开发控制台自己调查页面上的脚本/事件。我还强烈建议添加JavaScript和/或DOM标签以获取一些专家的帮助。 - Mikegrann
设置值的方式应该是这样的:ie.Document.getElementById("GetProductQuantitiesForAccessibleBranches").getElementsbyName("ProductGuid")(0).Value=Whatever - Ryan Wildry
如果您正在尝试提交表单,请尝试:ie.Document.Forms("GetProductQuantitiesForAccessibleBranches").Submit - Ryan Wildry
不幸的是,这也不起作用。代码行运行良好,没有错误,但提交操作未能成功执行。 - TheGuyOverThere
1
可能只是使用submit()而不是Submit()这么简单吗?不太确定VBA如何发送javascript命令。 - Steve
显示剩余7条评论
1个回答

3

尝试这个,我怀疑这不会是“完美”的答案,如果没有实际查看站点的话。但是,假设您没有任何框架/iframe(请确保没有),这应该能够找到正确的图像标签,并且应该单击它。

Public Sub Click_IMG_Tag()
    Dim Elements As Object
    Dim Element  As Object

    'Get a pointer to IE etc first, I'm assuming this is already done

    Set Elements = IE.Document.getElementsByTagName("img")

    For Each Element In Elements
        On Error Resume Next ' used to bypass elements that don't have .Title property
                             ' later, you should error handle this exception
        If Element.Title = "View Quantities At Other Locations" Then
            Element.Focus
            Element.Click
            Element.FireEvent ("OnClick")
            IELoad IE
            Exit For
        End If
    Next

End Sub

Public Sub IELoad(Browser As Object)
    While Browser.busy Or Browser.Readystate <> 4
        Application.Wait (Now() + TimeValue("00:00:01"))
        DoEvents
    Wend
End Sub

我正要回答类似的问题。我曾经遇到过同样的问题,然后不得不循环遍历所有按钮并检查具有特定标签的按钮,然后单击它。对我来说,这似乎是一个很好的答案。使用vba进行IE自动化本来就很麻烦。干得好! - Mafii
感谢您的反馈! - Ryan Wildry
谢谢你的尝试,我知道你已经花了很多精力帮我解决这个问题,但这也不起作用。代码运行良好,但网页没有更新。我真的完全没有主意了。 - TheGuyOverThere
没有任何框架? - Ryan Wildry
我没有看到任何问题。我能够与页面交互并提取信息,没有出现任何问题。每个页面上有50多个<img>标签,每个标签都是我已经从中提取数据的行的一部分。 - TheGuyOverThere

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接