如何使用VBA点击网站上的图片链接?

时间:2014-03-28 15:24:15

标签: javascript html excel vba excel-vba

我正在尝试自动化一个流程,我需要一次从内部网站为多个帐户提取一些文档。我在Excel 2010中使用VBA。到目前为止,我已经能够创建导航和登录的代码,直到我到达需要单击图像按钮的put。以下是相关的源代码。首先是一个功能:

    <script language="javascript" type="text/javascript"> 
    <!--
        function __doPostBack(eventTarget, eventArgument) {
                var theform;
                if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
                       theform = document.Form1;
                }
                else {
                       theform = document.forms["Form1"];
                }
                theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
                theform.__EVENTARGUMENT.value = eventArgument;
                theform.submit();
        }
    // -->
    </script>

然后跳过这个代码块,其中“Print.gif”是要点击的按钮:

        <LINK rel="stylesheet" type="text/css"
 href="/crystalreportviewers10/css/default.css">
<table id="CrystalReportViewer1" cellpadding="0" cellspacing="0"
   style="height:1043px;width:901px;Z-INDEX: 101;LEFT: 8px; POSITION: absolute; TOP: 8px">
    <tr><td>
    <div style="width: 901px; height: 1043px;position:relative">
    <div style="height:30px;width:901px;top:0px;left:0px;">
    <table class="crtoolbar" cellspacing=0 cellpadding=0><tr nowrap>
    <td nowrap width=16>&nbsp;</td>
    <td nowrap width=24px>
    <input type="image" name="CrystalReportViewer1:_ctl2:_ctl0" 
       title="Show/Hide Group Tree"
     onmouseover="this.src='/crystalreportviewers10/images/toolbar/grouptree_over.gif'"
     onmouseout="this.src='/crystalreportviewers10/images/toolbar/grouptree.gif'"
     src="/crystalreportviewers10/images/toolbar/grouptree.gif" alt="" border="0"
      style="height:24px;width:24px;" /></td>
    <td nowrap width=16>&nbsp;</td>
    <td nowrap width=24px>
    <img title="Print"
        onclick="javascript:__doPostBack('CrystalReportViewer1:_ctl2:_ctl3','')"
        onmouseover="this.src='/crystalreportviewers10/images/toolbar/print_over.gif'"
        onmouseout="this.src='/crystalreportviewers10/images/toolbar/print.gif'"
        src="/crystalreportviewers10/images/toolbar/print.gif" alt="" border="0"
        style="height:24px;width:24px;" /></td>

我尝试了几种不同的方法来激活“onclick”,但我遇到了麻烦,因为它看起来像传递给上面的函数导航到正确的页面。到目前为止,我已经尝试过:

        For Each obj In IE.Document.all
    '        MsgBox obj.innerHTML
            If InStr(obj.innerHTML, "Print") > 0 Then
            obj.Click
    '            Exit For
        End If
        Next

        For Each obj In IE.Document.all
            If obj.Name = "Print" Then
            obj.Click
            Exit For
            End If
        Next
到目前为止,还有一些让我失望的变种。对此的任何帮助都会很棒。感谢。

1 个答案:

答案 0 :(得分:0)

尝试下面的代码,让我们知道结果。

For Each obj In ie.document.all
      If InStr(obj.innerHTML, "Print") > 0 Then
         obj.document.parentWindow.execScript "javascript:__doPostBack('CrystalReportViewer1:_ctl2:_ctl3','')"
      End If
Next

OR

For Each obj In ie.document.all
    If InStr(obj.innerHTML, "Print") > 0 Then
        obj.Click
        obj.FireEvent ("onclick")
    End If
Next