Can't run my script without "On error resume next"



  • I've written a script to parse Names, Prices and Image links of different products from a certain page of redmart.com. It is working smoothly now. The only problem is that I've used "On error resume next" in my code. I don't like that line to have in my script but as soon as I take that line out the code breaks because some prices are none. If it were not for selenium I could have used length property within if statement to avoid that error. But, in this case Length property doesn't support. Size property could not bring any result either. I expect you will take a look into it. Thanks in advance.

    Sub Redmart_scraping()
    Dim driver As New WebDriver
    Dim posts As Object, post As Object
    
    driver.Start "Phantomjs", "https://redmart.com"
    driver.get "/bakery"
    Set posts = driver.FindElementsByCss("li.productPreview")
    On Error Resume Next
    For Each post In posts
        i = i + 1
        Cells(i, 1) = post.FindElementByCss("h4 > a").Text
        Cells(i, 2) = post.FindElementByCss("span[class^=ProductPrice__price]").Text
        Cells(i, 3) = post.FindElementByCss("img.showImage").Attribute("src")
    Next post
    End Sub

  • administrators

    @shahin2137 Ideal way to deal with this issue is to check if element exists using isElementPresent method but that method is removed now so you have to use proper error handling instead of ignoring the error using On Error Resume Next

    Read more about error handling here - http://www.cpearson.com/excel/errorhandling.htm



  • @ranjithkumar10 ,
    Thanks a ton. The great thing about creating any thread in the forum of your blog is that you never disappoint anyone from your support. Btw, according to the link you have provided above, if we get this feature [Try/Catch/Finally ] in vba then it will be awesome.



  • This one doesn't give any error. The css selector was faulty.

    Sub Redmart_scraping()
    Dim driver As New WebDriver
    Dim posts As Object, post As Object
    
    driver.Start "Phantomjs", "https://redmart.com"
    driver.get "/bakery"
    Set posts = driver.FindElementsByCss("li.productPreview")
    
    For Each post In posts
         i = i + 1: Cells(i, 1) = post.FindElementByCss("h4 > a").Text
        Cells(i, 2) = post.FindElementByCss("span[class^=ProductPrice]").Text
        Cells(i, 3) = post.FindElementByCss("img.showImage").Attribute("src")
    Next post
    End Sub

Log in to reply
 

Looks like your connection to Codingislove Forum was lost, please wait while we try to reconnect.