简体   繁体   中英

Getting HTML Source with Excel-VBA - Pop Up Window

The post below has a method to extract the HTML of a url using the following code.

Getting HTML Source with Excel-VBA

    Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .Send
        getHTTP = StrConv(.responseBody, vbUnicode)
    End With
End Function

The code works great, but my issue is that the website that I have (an internal website) has a pop-up window the first time I log into it. So accessing through this method gets me the HTML of the pop-up. How can I get beyond this window?

在此处输入图像描述

When operating the website manually, the first time I access the web page, the pop-up appears for a few seconds then it disappears and I have click on a button to login.


Update: Taking a deeper dive, the html returned is the login window for the site. Normally on this intranet based site, my first access in the day, I click a 'continue' button (no username/password) and it auto logs me in. And any time I access the site moving forward I can go directly to parts of the site without re-logging in. The link that I am using as my url is a direct link to a certain page on the site. Curious on how to get XMLHTTP logged into the site...

在此处输入图像描述

Here is the login - it is not a standard username and password. It is a username, password, department and city. Also a radio button to toggle between 2 settings. Then I would click on login.

I would firstly try this small modification, in order to exactly obtain the responseBody of the intermediary page (only one time):

Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .send
        Debug.print StrConv(.responseBody, vbUnicode):getHTTP = ""
    End With
End Function

And then I would copy the response from Immediate Window and use it next adapted code:

Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .send

        Do While StrConv(.responseBody, vbUnicode) = "the copied text from Immediate Window"
            DoEvents
        Loop
        getHTTP = StrConv(.responseBody, vbUnicode)
    End With
End Function

Or, if the response is too long, use Left("the text in discussion", 10) = "Whatever x"

Edited: Try transforming of your initial function in the next way, to include authentication:

Public Function getHTTP(ByVal url As String) As String
    With CreateObject("MSXML2.XMLHTTP")
        .Open "GET", url, False: .Send
        .setRequestHeader "Authorization", "Basic " & Base64Encode(Your_User & ":" & password)
        getHTTP = StrConv(.responseBody, vbUnicode)
    End With
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM