jatin verma
jatin verma

Reputation: 177

Get image src by class name in VBA

i am trying to get url of large image from a page

<ul id="etalage">
<li class=" product-image-thumbs" >
<img class="etalage_source_image_large" src="http://website.com/media/1200x1200/16235_1.jpg" title="" />
<img class="etalage_source_image_small" src="http://website.com/media/450x450/16235_1.jpg" title="" />
</li>
</ul>

my vba code is

Public Sub macro1()
Dim ie As Object
Dim name As String
  Do Until IsEmpty(ActiveCell)
    ActiveCell.Offset(0, 1).Value = "RUNNING"
    URL = Selection.Value
    Set ie = CreateObject("InternetExplorer.Application")
    With ie
      .Visible = 1
      .navigate URL
      While .Busy Or .readyState <> 4
        DoEvents
      Wend
    End With
Dim Doc As HTMLDocument
    Set Doc = ie.document
    ActiveCell.Offset(0, 1).Value = "ERROR"
    name = Trim(Doc.getElementsByClassName("product-image-thumbs")(0).innerText)
    ActiveCell.Offset(0, 2).Value = name
    ActiveCell.Offset(0, 1).Value = "successful"
    ActiveCell.Offset(1, 0).Select
    ie.Quit
  Loop
End Sub

my code giving blank cell...

also please suggest me how to run this macro faster.... i have 3000 url to work on.

Thanks in advance

Upvotes: 1

Views: 4559

Answers (1)

gembird
gembird

Reputation: 14053

According to the comments, try to speed the code up this way (untested code). The inner-text of the li element is empty string becasue there is no text inside of it, there is an image element but no text. HTH

Public Sub macro1()
    Dim ie As Object
    Dim name As String
    Dim Doc As HTMLDocument

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

    Do Until IsEmpty(ActiveCell)
        ActiveCell.Offset(0, 1).Value = "RUNNING"
        url = Selection.Value
        ie.navigate url
        While ie.Busy Or ie.readyState <> 4
            DoEvents
        Wend
        Set Doc = ie.document
        ActiveCell.Offset(0, 1).Value = "ERROR"
        name = Trim(Doc.getElementsByClassName("product-image-thumbs")(0).innerText)
        ActiveCell.Offset(0, 2).Value = name
        ActiveCell.Offset(0, 1).Value = "successful"
        ActiveCell.Offset(1, 0).Select
    Loop

    ie.Quit
End Sub

To get the src of all the images try using querySelectorAll method.

Dim img, imgs As IHTMLDOMChildrenCollection, i
Set imgs = Doc.querySelectorAll("li[class~='product-image-thumbs']>img")

For i = 0 To imgs.Length - 1
    Set img = imgs.item(i)
    Debug.Print img.getAttribute("src")
Next

See CSS attribute selectors.

EDIT: If there are more img elements inside if the li.product-image-thumbs element then you have more possibilities how to get the right one img.

  • Get img which is placed immediately after the li :

"li[class~='product-image-thumbs']+img"

  • Get img inside of li by class name :

"li[class~='product-image-thumbs'] img[class~='etalage_source_image_small']"

Upvotes: 1

Related Questions