I'd like to extract this href from that particular class
<tr class="even">
<td>
<a href="/italy/serie-a-2015-2016/">Serie A 2015/2016</a>
</td>
This is what I wrote:
Sub ExtractHrefClass()
Dim ie As Object
Dim doc As HTMLDocument
Dim class As Object
Dim href As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate Range("D8")
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Set doc = ie.document
Set class = doc.getElementsByClassName("even")
Set href = class.getElementsByTagName("a")
Range("E8").Value = href
ie.Quit
End Sub
But unfortunately there is a mistake Object doesn't support this property or method (Error 438) on the line:
Set href = class.getElementsByTagName("a")
UPDATE 1
I modified the code as per @RyszardJędraszyk answer, but no output come out O_o Where am I doing wrong?
Sub ExtractHrefClass()
Dim ie As Object
Dim doc As HTMLDocument
Dim href As Object
Dim htmlEle As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.navigate Range("D8")
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE And ie.Busy = False
Set doc = ie.document
Set href = doc.getElementsByTagName("a")
For Each htmlEle In href
If htmlEle.className = "even" Then
Range("E8").Value = htmlEle
End If
Next
ie.Quit
End Sub
UPDATE 2
As @dee requested in comment, there is the code from the web page http://www.soccer24.com/italy/serie-a/archive/
<tbody>
<tr>
<td>
<a href="/italy/serie-a/">Serie A 2016/2017</a>
</td>
<td></td>
</tr>
<tr class="even">
<td>
<a href="/italy/serie-a-2015-2016/">Serie A 2015/2016</a>
</td>
<td>
<span class="team-logo" style="background-image: url(/res/image/data/UZbZIMhM-bsGsveSt.png)"></span><a href="/team/juventus/C06aJvIB/">Juventus</a>
</td>
</tr>
<tr>
<td>
<a href="/italy/serie-a-2014-2015/">Serie A 2014/2015</a>
</td>
<td>
<span class="team-logo" style="background-image: url(/res/image/data/UZbZIMhM-bsGsveSt.png)"></span><a href="/team/juventus/C06aJvIB/">Juventus</a>
</td>
</tr>
I need only to extract that line: /italy/serie-a-2015-2016/
querySelectorAll
orquerySelector
can be used here to select theanchor
elemets inside of thetr
with the specificclass
and then withgetAttribute("href")
thehref-attribute
can be retrieved. HTH.Try:
Make sure that proper libraries are checked in references (Microsoft HTML Object Library).
Are you sure that
doc.getElementsByClassName("even")
works? It is not listed here: https://msdn.microsoft.com/en-us/library/aa926433.aspx as available method.I always first use
getElementsByTagName
and make a condictionIf htmlEle.className = "even" then
.Also add the following:
ie.readyState = READYSTATE_COMPLETE and ie.busy = False
. Still if it is some AJAX based website it can be not enough to determine that website has fully loaded (from the link guessing it could be flashscore.com where you need to track elements on the website informing about its loading status).This worked for me:
The procedure you need might look like: