Seale
Seale

Reputation: 63

VBA closing IE javascript popup

The below code opens an instance of InternetExplorer and downloads odds. It works fine but occasionally a pop-up window appears which causes the code to not work. Any help on how to navigate the below pop-up (i.e. click 'continue to oddschecker') when the pop-up does appear?

<a class="continue beta-callout js-close-class" onclick='s_objectID="javascript:void(0)_9";return this.s_oc?this.s_oc(e):true' href="javascript:void(0)">Continue to Oddschecker</a>

Full code:

Sub Oddschecker()

Dim ie, wp As Object
Dim i As Integer

Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
ie.Navigate "https://www.oddschecker.com/horse-racing/racing-coupon"

Do While ie.Busy
    DoEvents
Loop

Do While ie.ReadyState <> 4
    DoEvents
Loop

Set wp = ie.Document

'Application.ActiveSheet.UsedRange.ClearContents
Application.Worksheets("sheet1").UsedRange.ClearContents

i = 2
For Each rw In wp.getElementsByTagName("table")(0).getElementsByTagName("tr")
    If rw.className = "date" Then
        Worksheets("sheet1").Range("A1") = rw.innerText
    ElseIf rw.className = "fixture-name" Then
        i = i + 1
        Worksheets("sheet1").Range("A" & i) = rw.getElementsByTagName("td")(0).innerText
        i = i + 1
    ElseIf rw.className = "coupons-table-row match-on" Then
        For Each od In rw.getElementsByTagName("p")
            If InStr(od.innerText, "(") <> 0 Then
                Worksheets("sheet1").Range("A" & i) = Trim(Left(od.innerText, InStr(od.innerText, "(") - 1))
                np = Trim(Right(od.innerText, Len(od.innerText) - InStr(od.innerText, "(")))
                Worksheets("sheet1").Range("B" & i) = Left(np, Len(np) - 1)
                i = i + 1
            Else
                Worksheets("sheet1").Range("A" & i) = Trim(od.innerText)
                i = i + 1
            End If
        Next od

    End If
Next rw

ie.Quit

Range("A1:B" & i).WrapText = False
Columns("A:B").EntireColumn.AutoFit

Set wp = Nothing
Set ie = Nothing


End Sub

Upvotes: 0

Views: 1355

Answers (1)

SIM
SIM

Reputation: 22440

If you wish to continue with that page (navigating to that popup page), you can try like:

Dim HTML As HTMLDocument, addcheck As Object

While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend ''(You can write it the way you feel comfortable)
Set HTML = IE.document  ''place this line after the prevous line

Set addcheck = HTML.querySelector("#promo-modal a.continue")
If Not addcheck Is Nothing Then
    addcheck.Click
End If

But, that is not a good idea cause it will lead you to some page where you might need to do some activity to get back on this data ridden page.

I suppose you should get rid of that popup blocker by ticking the cross button located on the top right area and continue to do what you are doing:

Dim HTML As HTMLDocument, addcheck As Object

While IE.Busy = True Or IE.readyState < 4: DoEvents: Wend ''(You can write it the way you feel comfortable)
Set HTML = IE.document  ''place this line after the prevous line

Set addcheck = HTML.querySelector("#promo-modal span[title='Close")
If Not addcheck Is Nothing Then
    addcheck.Click
End If

If I didn't understand what your intention was, do let me know. Thanks.

Upvotes: 1

Related Questions