在IE中点击图片按钮

时间:2018-10-31 05:56:33

标签: vba internet-explorer web-scraping

我正在研究“自动化IE”项目,但是在单击“图像”按钮时陷入了困境……

我正在尝试从网站上后台处理一个excel报告,对于同样的情况,我需要单击“生成”按钮(type="submit")`` then one message will appear(成功后台处理报告)then only I can click on download button(type =“ image”)`

屏幕截图

Below is the Screenshot

下面是“生成”按钮的HTML源代码

<td align="right">
<input type="submit" name="ctl00$ContentPlaceHolder1$btnGenerate" value=" Generate "
onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ctl00$ContentPlaceHolder1$btnGenerate&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" 
id="ctl00_ContentPlaceHolder1_btnGenerate" tabindex="4" title="Click Here" class="dxbButton_Glass">
</td>

以下是消息的HTML源代码

<td colspan="2">
<span id="ctl00_ContentPlaceHolder1_lblMsg" style="color:Blue;font-weight:bold;">Report Spooled Successfully</span>
</td>

下面是下载按钮的HTML源代码(type="image"

<td align="right" visible="false">
click here to Download&nbsp;
<input type="image" name="ctl00$ContentPlaceHolder1$lnkDownload" id="ctl00_ContentPlaceHolder1_lnkDownload" tabindex="7" title="Download" src="../../Images/save.gif" alt="Download" style="border-width:0px;">
</td>

使用我的代码,我可以单击Generate按钮,但是在出现消息后单击Download按钮时遇到困难,已经尝试循环直到满足条件,并尝试Element.innertext = message但没有运气。

下面是我的VBA代码

Sub KBOSS_Brokerage_Process_Status()
    Set Browser = CreateObject("internetexplorer.application")
    Browser.Visible = false
    Browser.navigate ("my url")
    Set mymsg = Browser.document.getElementById("ctl00_ContentPlaceHolder1_lblMsg")
    For Each elem In mymsg.getElementsByTagName("span")
        If elem.innerText = " Report Spooled Successfully " Then Exit For
    Next elem
    Browser.document.getElementById("ctl00_ContentPlaceHolder1_lnkDownload").Click
    Browser.Quit
    Set Browser = Nothing
End Sub

谢谢!

交叉帖子: https://chandoo.org/forum/threads/vba-code-to-select-an-option-from-html-drop-down-using-value-rather-than-index.37707/#post-228291

1 个答案:

答案 0 :(得分:0)

因此,这可能无法解决您的问题,但至少会引入适当的页面加载等待时间,包括在每次单击事件之后,以及出现后的文本的定时循环。我还添加了注释掉的FireEvent,以防触发onclick。您无需将Browser设置为空。潜艇完成时将其丢弃。当Object变量通过超出范围释放其引用时,运行时将调用目标对象的Release方法以减少对象的引用计数。

Option Explicit

Public Sub KbossBrokerageProcessStatus()
    Dim browser As Object, t As Date, ele As Object, testValue As String
    Const MAX_WAIT_SEC As Long = 5

    Set browser = CreateObject("InternetExplorer.Application")
    With browser
        .Visible = True
        .navigate2 "myUrl"

        While .Busy Or .readyState < 4: DoEvents: Wend

        With .document

            .getElementById("ctl00_ContentPlaceHolder1_btnGenerate").Click '.FireEvent("onclick")

            While browser.Busy Or browser.readyState < 4: DoEvents: Wend

            Do
                DoEvents
                On Error Resume Next
                Set ele = .getElementById("ctl00_ContentPlaceHolder1_lblMsg")
                testValue = ele.innerText
                On Error GoTo 0
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            Loop While testValue  <> "Report Spooled Successfully"

            If ele Is Nothing Or testValue  <> "Report Spooled Successfully"   Then Exit Sub

            .document.getElementById("ctl00_ContentPlaceHolder1_lnkDownload").Click

            While browser.Busy Or browser.readyState < 4: DoEvents: Wend

        End With

        .Quit
    End With
End Sub