Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

How to use excel vba to click on interactive dialog pop-up?

$
0
0

I am trying to use excel vba to navigate and export data from this website. I am able to click on 2018, 2019 buttons and the setting option, but unable to click the 'export data' option with vba. I attach my code below for your reference.

    Option Explicit

    Sub GetURLOfFrame()

    Dim IE As New SHDocVw.InternetExplorer
    Dim webpage As MSHTML.HTMLDocument

    IE.Visible = True
    IE.navigate "https://www.epa.gov/fuels-registration-reporting-and-compliance-help/rin-trades-and-price-information"
        Dim t As Date, ele As Object 'waiting
        Const MAX_WAIT_SEC As Long = 10 '<==Adjust wait time

        While IE.Busy Or IE.readyState < 4: DoEvents: Wend
        t = Timer
        Do
            DoEvents
            On Error Resume Next
                Set ele = IE.document.getElementById("show-service-popup-dialog")
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            On Error GoTo 0
        Loop While ele Is Nothing
        Application.Wait (Now + TimeValue("0:00:10"))

    Set webpage = IE.document
    Dim wkb As Workbook, wksRIN As Worksheet, wksFrames As Worksheet
    Set wkb = ThisWorkbook
    Set wksRIN = wkb.Sheets("RIN Trades and Prices")

    Dim Frame As MSHTML.IHTMLElement
    Set Frame = webpage.getElementsByTagName("iframe")(1)


    Dim URL As String
    URL = Frame.getAttribute("src") 'storing the link to the frame

    IE.navigate URL 'goes to the iframe site
        While IE.Busy Or IE.readyState < 4: DoEvents: Wend
        t = Timer
        Do
            DoEvents
            On Error Resume Next
                Set ele = IE.document.getElementById("content")
                If Timer - t > MAX_WAIT_SEC Then Exit Do
            On Error GoTo 0
        Loop While ele Is Nothing
        Application.Wait (Now + TimeValue("0:00:10"))

    Dim htmlarticle As MSHTML.IHTMLElement
    Set htmlarticle = webpage.getElementsByTagName("article")(0)

    Application.Wait (Now + TimeValue("0:00:02"))

    Dim buttons As MSHTML.IHTMLElementCollection
    Set buttons = htmlarticle.getElementsByClassName("lui-buttongroup ng-scope")(0).getElementsByTagName("button")
    buttons(buttons.Length - 2).Click 'clicking the second last year
    buttons(buttons.Length - 1).Click 'clicking the latest year

    Application.Wait (Now + TimeValue("0:00:02"))

    Dim SettingButton As MSHTML.IHTMLElement
    Set SettingButton = htmlarticle.getElementsByClassName("cl-icon cl-icon--cogwheel cl-icon-right-align")(0)
    SettingButton.Click

    Dim SettingOptions As MSHTML.IHTMLElementCollection, ExportDataButton As MSHTML.IHTMLElement, button As MSHTML.IHTMLElement
    Set SettingOptions = webpage.getElementsByClassName("qv-contextmenu ng-scope")(0).getElementsByTagName("li")

    Application.Wait (Now + TimeValue("0:00:05"))

    Set ExportDataButton = webpage.getElementsByClassName("qv-contextmenu ng-scope")(0).getElementsByTagName("li")(0)
    ExportDataButton.Focus
    ExportDataButton.Click

    IE.Quit
    Set IE = Nothing

End Sub

When I run 'ExportDataButton.Click', nothing seems to happen and I am not sure the reason why. It is also not possible to scrape from the data in the table directly as the the values of innertext of the td are dynamic and changes as you scroll down the table.

Unable to click on 'Export Data'


Viewing all articles
Browse latest Browse all 88854

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>