Patrik Fröhler
Patrik Fröhler

Reputation: 1291

Visual basic copy html link from website (getElementsByClassName)?

Hi I'm new to VB and currently working with it in Excel and I'm having problems I want to go to a website then get a link from that website and then print that link.

I already know how to do the "go to the website and get the innertext by getElementsById":

Dim ie As InternetExplorer, doc As HTMLDocument, quote As String


Sub MsgboxTest()

    Set ie = CreateObject("InternetExplorer.Application")
    ie.Visible = True

        ie.navigate "http://www.patan77.com/"


        Do
            DoEvents
            Loop Until ie.readyState = READYSTATE_COMPLETE

            Set doc = ie.document


            quote = doc.getElementById("date").innerText



           Debug.Print quote



End Sub

This just gets the text (the date) from an div id if I want it to print the link that I get from going to the bottom in the page hovering over "Other Stuff" then the actual link from CinebenchEstimator:

IMG: http://www.patan77.com/screenshot/print_link_2.JPG

in this case I want it to print this: http://www.patan77.com/Cinebench_Estimator/Cinebench_Estimator.html

(I'm just using my site as an example here to show what I want to do)

I'm guessing I would need to do something like this:

quote = doc.getElementById("mail").getElementsByClassName("menu")(0).getElementsByTagName("ul")(0).getElementsByTagName("li")(0).getElementsByTagName("ul")(0).getElementsByTagName("li")(3).getAttribute("href")

but that is obviously not working, so what I'm I missing / doing wrong?

Thanks in advance.

(:

Upvotes: 1

Views: 2145

Answers (1)

pete
pete

Reputation: 25081

The only problem is that:

doc.getElementById("mail") _
    .getElementsByClassName("menu")(0) _
    .getElementsByTagName("ul")(0) _
    .getElementsByTagName("li")(0) _
    .getElementsByTagName("ul")(0) _
    .getElementsByTagName("li")(3)

returns a li element which does not have an href attribute to get.

You want the first child of that li element:

doc.getElementById("mail") _
    .getElementsByClassName("menu")(0) _
    .getElementsByTagName("ul")(0) _
    .getElementsByTagName("li")(0) _
    .getElementsByTagName("ul")(0) _
    .getElementsByTagName("li")(3) _
    .Children(0).getAttribute("href")

This will return the href of Cinebench_Estimator/Cinebench_Estimator.html. Prepend the URL base ("http://www.patan77.com/") and you'll be all set.

Upvotes: 1

Related Questions