PDA

View Full Version : For each class("addressbox") in html document follow link



b.hill
03-14-2013, 07:18 PM
I am trying to click on several links in a web page. They are all within classes named "addressbox". There are multiple classes with the name "addressbox". The names of the links are different each time so I cannot name them in the code.

I cannot get my code to click on the link in each class, but the logic is this:

Sub GetData
'code to go to website (this part is working)
For each class that equals "addressbox" in the html document
Click on the second href link
'Extract data (this part is working)
ie.GoBack (this part should work but it's untested)
Next
End Sub


Here is part of the source code with one of the "addressbox" classes. In this case, I would like to follow the href="/669738/detail" link but I would not always know the name of the href:

<div class="addressbox" id="adbox0">
<div class="adline">
<table>
<tr>
<td valign="top">
<input type="checkbox" name="homecheckbox" value="11491|GDAR"/>
</td>
<td valign="top">
<div class="newIcon fLeft tooltipImage" id="newIcon0" title="New">NEW</div>
<script type="text/javascript">
createTooltip('newIcon0', '<p>Within 3 <a href="/check?src=searchResultsNewIcon">Here</a></p>');
</script>

<a href="/669738/detail">Click here</a>
</td>
</tr>
</table>
</div>

mohanvijay
03-14-2013, 10:53 PM
class in html indicates CSS style sheet not OOP class

Try below code



Sub Click_href()
Dim IE_Main As SHDocVw.InternetExplorer
Dim H_Doc As MSHTML.HTMLDocument
Dim EE_Href As MSHTML.HTMLAnchorElement

Set IE_Main = New SHDocVw.InternetExplorer
IE_Main.navigate "http://www.google.com" 'Set your url here
IE_Main.Visible = True
Do Until IE_Main.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set H_Doc = IE_Main.document
For Each EE_Href In H_Doc.all

If EE_Href.href = "/669738/detail" Then
EE_Href.Click
Exit For
End If
Next
Set EE_Href = Nothing
Set H_Doc = Nothing
Set IE_Main = Nothing
End Sub

b.hill
03-15-2013, 05:44 PM
I wish I could do that but the "669738" in the href changes each time. The "/detail" however is always the same.

I have stepped through the following code and it recognizes the class "addressbox" each time but still won't click on the link ending in "/detail".

Any suggestions?

Click_href()

Dim oHTML_Element As IHTMLElement
Dim sURL As String
Dim EE_HREF As MSHTML.HTMLAnchorElement
sURL = "http://www.google.com" 'Set your url here
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.Visible = True
oBrowser.navigate sURL
Do
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLdoc = oBrowser.document

For Each EE_HREF In HTMLdoc.getElementsByClassName("addressbox")
If (InStr(EE_HREF, "/detail")) Then
'If InStr(EE_HREF, "/detail") > 0 Then
'oBrowser.navigate EE_HREF.href
EE_HREF.Click
Exit For
'Extract data into Excel
oBrowser.GoBack
End If
Next
End Sub