【问题标题】:Form.Submit does not go through when using VBA使用VBA时Form.Submit不通过
【发布时间】:2016-12-30 21:05:26
【问题描述】:

我有一个从中提取数据的网页。除了单击图像元素然后提交表单并创建包含数据的弹出窗口之外,我可以使用 VBA 完成所有操作。

图像元素中的一个属性称为“productguid”,具有字符串值 =

“a1​​2-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>

基于此,我假设 image 元素中的 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

根据 Locals 窗口,所有的 Javascript 事件都是Null。两行代码运行没有任何错误,但网页没有更新。我在这里遗漏了什么吗?

我也尝试过使用.Click 方法单击图像元素,但这也无济于事。

该网页受密码保护,因此我无法公开发布网址。

更新:

这里是标签中的 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 标记以吸引一些专家。
  • 哦,好的,谢谢。我厌倦了使用 Chrome Dev,但我很难理解我在看什么......如果我点击 Networks 选项卡,在 Initiator 列中我会看到一个脚本,然后当我将鼠标悬停在它上面时,整个列表不同的东西出现。我应该如何处理这些信息?
  • 设置值类似于:ie.Document.getElementById("GetProductQuantitiesForAccessibleBranches").getElementsbyName("ProductGuid")(0).Value=Whatever 应该可以工作
  • 更改值很好,它可以做到这一点。提交是问题。发布请求根本不通过。
  • 会不会像使用 submit() 而不是 Submit 一样简单?不是 100% 确定 VBA 如何发送 javascript 命令..

标签: javascript html vba dom browser-automation


【解决方案1】:

试一试,我怀疑这将是“完美”的答案,而无需实际查看该网站。但是,这应该会找到正确的图像标签,假设您没有任何框架/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 自动化是一件很痛苦的事情。干得好!
  • 感谢您的反馈!
  • 感谢您的尝试,我知道您已经花费了很多精力来帮助我解决这个问题,但这也不起作用。代码运行良好,但网页没有更新。我真的完全没有想法。
  • 没有任何框架?
  • 我看不到。我能够毫无问题地从页面进行交互和提取信息。每个页面上有超过 50 个&lt;img&gt; 标签,每个标签都是我已经从中提取数据的行的一部分
猜你喜欢
  • 2013-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多