How do I click a link with a specific class in a web page loaded using Excel VBA?

thusarix

New Member
Joined
Jun 14, 2017
Messages
9
I want to click a href link that is dynamic.I have really very little clue as to what am i doing, as i have little knowledge of programing(still doing VBA tutorials).I am not sure if it can be done with VBA or i need JAVA(of which i know almost zero). A push in the right direction would be great.

Code:
[COLOR=#E56717][B]Sub[/B][/COLOR] useClassnames()
     [COLOR=#151B8D][B]Dim[/B][/COLOR] row            [COLOR=#151B8D][B]As[/B][/COLOR] [COLOR=#F660AB][B]Long[/B][/COLOR]
     [COLOR=#151B8D][B]Dim[/B][/COLOR] link            [COLOR=#151B8D][B]As[/B][/COLOR] IHTMLElementCollection
     [COLOR=#151B8D][B]Dim[/B][/COLOR] ie              [COLOR=#151B8D][B]As[/B][/COLOR] InternetExplorer

     [COLOR=#151B8D][B]Set[/B][/COLOR] ie = [COLOR=#E56717][B]New[/B][/COLOR] InternetExplorer

     [COLOR=#8D38C9][B]With[/B][/COLOR] ie
         .navigate [COLOR=#800000]"https://www.unibet.ro/betting#filter/all/all/all/all/in-play"[/COLOR]
         .Visible = [COLOR=#00C2FF][B]True[/B][/COLOR]

         [COLOR=#8D38C9][B]Do[/B][/COLOR] [COLOR=#8D38C9][B]While[/B][/COLOR] ie.readyState <> READYSTATE_COMPLETE
             [COLOR=#8D38C9][B]DoEvents[/B][/COLOR]
         [COLOR=#8D38C9][B]Loop[/B][/COLOR]
     [COLOR=#8D38C9][B]End[/B][/COLOR] [COLOR=#8D38C9][B]With[/B][/COLOR]

     [COLOR=#8D38C9][B]Do[/B][/COLOR] [COLOR=#8D38C9][B]Until[/B][/COLOR] ie.document.getElementsByClassName([COLOR=#800000]"KambiBC-event-item__participants-container"[/COLOR]).Length > 0
         [COLOR=#8D38C9][B]DoEvents[/B][/COLOR]
     [COLOR=#8D38C9][B]Loop[/B][/COLOR]

     row = 1

     [COLOR=#8D38C9][B]If[/B][/COLOR] Cells(row, 1).Value = 0  [COLOR=#8D38C9][B]Then[/B][/COLOR]
         [COLOR=#151B8D][B]Set[/B][/COLOR] link = ie.document.getElementsByTagName([COLOR=#800000]"a"[/COLOR])
         [COLOR=#8D38C9][B]For[/B][/COLOR] [COLOR=#8D38C9][B]Each[/B][/COLOR] l [COLOR=#8D38C9][B]In[/B][/COLOR] link
             [COLOR=#8D38C9][B]If[/B][/COLOR] link.className = [COLOR=#800000]"KambiBC-event-item__link KambiBC-js-event-item-link"[/COLOR] [COLOR=#8D38C9][B]Then[/B][/COLOR]
                 link.Click
                 [COLOR=#E56717][B]Exit[/B][/COLOR] [COLOR=#8D38C9][B]For[/B][/COLOR]
             [COLOR=#8D38C9][B]End[/B][/COLOR] [COLOR=#8D38C9][B]If[/B][/COLOR]
         [COLOR=#8D38C9][B]Next[/B][/COLOR]
         row = row + 1
     [COLOR=#8D38C9][B]End[/B][/COLOR] [COLOR=#8D38C9][B]If[/B][/COLOR]
[COLOR=#8D38C9][B]End[/B][/COLOR] [COLOR=#E56717][B]Sub[/B][/COLOR]

FYI i did ask this question on stackoverflow, and chandoo, but got no answer.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi,

I'd like to learn the answer as well, so I have to mark the post.

As an untested proposal, try

Rich (BB code):
If link.className = "KambiBC-event-item__link KambiBC-<acronym title="JavaScript">js</acronym>-event-item-link" Then
                 
set myLink = l
myLink.Click
                 Exit For
             End If


regards
 
Upvote 0
Hi,

I'd like to learn the answer as well, so I have to mark the post.

As an untested proposal, try

Rich (BB code):
If link.className = "KambiBC-event-item__link KambiBC-<acronym title="JavaScript">js</acronym>-event-item-link" Then
                 
set myLink = l
myLink.Click
                 Exit For
             End If


regards



Run-time error '438':
Object doesn't support this property or method
on this line:
Code:
If link.className = "KambiBC-event-item__link KambiBC-js-event-item-link" Then

But why? Since i am using IE11.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top