【问题标题】:VBA Web Scraping: Clicking on Download Bar (Save file) when ie.visible=FalseVBA Web Scraping:当ie.visible = False时单击下载栏(保存文件)
【发布时间】:2021-10-15 12:07:47
【问题描述】:

IE.visible=False时有什么方法可以点击保存按钮吗?

我尝试了这个仅在 ie.visible =True 时有效的选项 Excel VBA to Save As from IE 11 Download Bar

通用示例:

Sub scrape_ex()
   
    
    Dim oHTML_Element As IHTMLElement
    Dim oHTML_Element_signo As IHTMLElement
    Dim oBrowser As InternetExplorer
    Dim ie As Variant
    
    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = False
    ie.navigate "URL"
    
        While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
            DoEvents
        Wend
        
    For Each oHTML_Element In ie.document.getElementsByClassName("class")
        If oHTML_Element.Title = "my_download" Then
              oHTML_Element.Click
              Application.Wait (Now + TimeValue("00:00:03"))' Download Bar starts
                Exit For
        
        End If
    Next
''clicking on Save that work with ie.visible=False ???

编辑:

点击页面中的按钮效果很好,在网页中不是问题。我想在IE下载栏开始时点击“保存”(IE.visible=False,所以我不能使用SendKeys):

【问题讨论】:

    标签: vba web-scraping internet-explorer


    【解决方案1】:

    如果您只是使用自动化来单击页面中的按钮,那么它应该可以工作,即使在 ie.Visible = False 的情况下也是如此。

    这是我的简单测试,效果很好:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>test</title>
    </head>
    <body>
    
        <button type="button" id="ieDown" onclick="onSaveInIE()">In IE</button>
    
        <script>
            function onSaveInIE() {
                var csvWindow = openNewWindowObj();
                csvWindow.document.execCommand('SaveAs', null, 'abc.html');
                csvWindow.close();
            }
    
            function openNewWindowObj() {
                var newWindowObj = window.open("window.html", "New popup Window", 'height=200,width=150')
                newWindowObj.focus();
    
                return newWindowObj;
            }
        </script>
    </body>
    </html>
    

    VBA 代码:

    Sub scrape_ex()
       
        Dim oHTML_Element As IHTMLElement
        Dim oHTML_Element_signo As IHTMLElement
        Dim oBrowser As InternetExplorer
        Dim ie As Variant
        
        Set ie = CreateObject("InternetExplorer.Application")
        ie.Visible = False
        ie.navigate "https://localhost:44315/Index.html"
        
        
            While ie.readyState <> READYSTATE_COMPLETE And ie.readyState <> READYSTATE_LOADED
                DoEvents
            Wend
            
        For Each oHTML_Element In ie.document.getElementsByTagName("button")
            If oHTML_Element.ID = "ieDown" Then
                  oHTML_Element.Click
                  Application.Wait (Now + TimeValue("00:00:03")) ' Download Bar starts
                    Exit For
            
            End If
        Next
    End Sub
    

    注意:我不确定您的页面是如何设计的,因此我修改了一些 VBA 代码以获取此测试中的页面元素。

    编辑

    如果你需要使用自动化来点击`IE Application UI中的一个按钮,当它不可见时,这是不可能的。因为键将转到具有焦点的窗口。如果窗口不可见,则无法获得焦点,因此无法接收键。

    【讨论】:

    • 已编辑问题
    • 我已经编辑了我的答案。总之,你无法达到这样的要求。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-30
    相关资源
    最近更新 更多