Zakiirim
Zakiirim

Reputation: 81

Click on HTMLelement if condition is satisfied

i'm wondering how can i manage to click on an html element through VBA if another condition is satisfied. To make it clear, i will show you a short example:

i need to analize data in a specific quarter ('let's say i need Q2) and for each quarter html elements are grouped in class=module_item in the same page (EXAMPLE) .

The website is https://investor.apple.com/investor-relations/default.aspx

My code is this:

Dim IE As New InternetExplorer
Dim iedoc As MSHTML.HTMLDocument
Dim element As MSHTML.IHTMLElement
Dim elements As MSHTML.IHTMLElementCollection
Dim quarter As MSHTML.IHTMLElement
Dim freport As MSHTML.IHTMLElement

With IE:
    .navigate "https://investor.apple.com/investor-relations/default.aspx"
    .Visible = True
End With

Do While IE.readyState <> READYSTATE_COMPLETE
Loop

Set iedoc = IE.document

Set elements = iedoc.getElementsByClassName("module_item")
Set quarter = iedoc.getElementsByClassName("module-financial_year-text")
Set freport = iedoc.getElementsByClassName("module_link module_link-statement")

For Each element In elements:
    If quarter.innerText = "Q2" Then
    freport.Click
    End If
    Debug.Print element.innerText
    Next element


End Sub

Can you help me?

Upvotes: 4

Views: 130

Answers (1)

QHarr
QHarr

Reputation: 84475

You can use css attribute = value selector, with * contains operator, to test for presence of href attribute containing the quarter description

Dim link As Object, targetQtr As String, cssSelector As String, quarter As Object

targetQtr = "second"
cssSelector = "[href*='apple-reports-" & targetQtr & "-quarter-results/']"
Set quarter = ie.document.querySelectorAll(cssSelector)

If Not quarter.Length = 0 Then
    quarter.Item(0).Click
Else
    MsgBox "Quarter not found"
End If

Also, use a proper page load wait:

While IE.busy Or IE.readyState <> 4: DoEvents: Wend

You could also gather all quarters and their links in a dictionary so you can .navigate to any specific quarter by using the appropriate quarter key e.g. "second" to retrieve the associated url. This means you can also test if key exists. The advantage here is you have all the links ready and don't have to go back and forth to landing page.

Option Explicit

Public Sub test()
    Dim ie As Object, quarters As Object, quarterLinks As Object, targetQuarter As String
    Dim link As String, i As Long, arr() As String, key As Variant

    Set ie = CreateObject("InternetExplorer.Application")
    targetQuarter = "second"

    With ie
        .Visible = True
        .navigate2 "https://investor.apple.com/investor-relations/default.aspx"
        While .busy Or .readystate <> 4: DoEvents: Wend
        Set quarters = .document.querySelectorAll("[href*='-quarter-results/'].module_link-news")

        Set quarterLinks = CreateObject("Scripting.Dictionary")

        For i = 0 To quarters.Length - 1
            link = quarters.Item(i).href
            arr = Split(link, "-")
            quarterLinks(arr(2)) = link
        Next

        If quarterLinks.exists(targetQuarter) Then .navigate2 quarterLinks(targetQuarter)

        While .busy Or .readystate <> 4: DoEvents: Wend

        Stop                                     '<=delete me later
        .Quit
    End With
End Sub

Upvotes: 2

Related Questions