Error handling advice for webscrapping

BalloutMoe

Board Regular
Joined
Jun 4, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hello
I need some help with the code below, Sometimes the page does not login, sometimes the iframe does not load, I am looking for a way to handle this error and attempt again and not resume, if not then exit and login in again.
In bold are the areas where sometimes it crashes. Any help or guidance would be appreciated. Thank you



VBA Code:
Sub XLPartsSwitchAccount()
ClickedInvoices = 0

'SeleniumWebElements
Dim UserInput As Selenium.WebElement, PasswordID As Selenium.WebElement, SubmitBtn As Selenium.WebElement
Dim ElementTable As Selenium.WebElements
Dim web_table As Selenium.WebElement

'Long/Array
Dim ShopFldArr As Variant, ShopName As String

Dim i As Long, n As Long, ShopPin As Long
Dim Attempt As Integer

OpenUpAgain:
Set CD = New Selenium.ChromeDriver

CD.Start
CD.Get Website
CD.Window.Maximize
Attempt = 0
    Do
    Attempt = Attempt + 1
    If CD.IsElementPresent(By.ID("username")) Then
    Exit Do
    End If
    
    If Attempt > 30 Then
    Attempt = 0
    Set CD = Nothing
    GoTo OpenUpAgain:
    End If
    
    Loop

TryAgain:


'Find UserName
Set UserInput = CD.FindElementById("username")
UserInput.SendKeys  USERNAME HERE
CD.Wait 100
'Find PasswordID
Set PasswordID = CD.FindElementById("password")
PasswordID.SendKeys  USERNAME HERE
CD.Wait 100

Set SubmitBtn = CD.FindElementById("mui-1")
SubmitBtn.Click
CD.Wait 2000
[B]SOMETIMES IT LOGS IN BUT THE PAGE DOES NOT ACTUALLY LOG IN[/B]

Attempt = 0
    Do
    CD.Wait 1000
    Attempt = Attempt + 1
    Debug.Print Attempt
    If CD.IsElementPresent(By.XPath("/html/body/div[6]/div/div/div[3]/button")) Then   '[B]Find a better way here[/B]
    CD.FindElementByXPath("/html/body/div[6]/div/div/div[3]/button").Click '[B]Find a better way here[/B]
    Exit Do
    ElseIf CD.IsElementPresent(By.Class("container-fluid")) Then
    Exit Do
    End If
    
    If Attempt = 100 Then
    Attempt = 0
    GoTo TryAgain
    End If
    
    Loop

'containerHead
Attempt = 0


ShopFldArr = Folders.ListObjects("Folders").DataBodyRange.value


For ShopPin = LBound(ShopFldArr) To UBound(ShopFldArr)


    If ShopFldArr(ShopPin, 5) <> "" Then 'Check if shop is in xlParts
    ShopName = ShopFldArr(ShopPin, 2)
        
    CD.Get Website
    
    Attempt = 0
    Do
    Attempt = Attempt + 1
    If CD.IsElementPresent(By.Class("tab-content")) Then
    CD.Wait 2000
    Exit Do
    End If
    
    If Attempt = 30 Then
    Attempt = 0
    CD.Refresh
    End If
    
    Loop
    
    CD.Wait 3000
  
    CD.FindElementByXPath("//*[@id='left-tabs-example-tab-6']").Click

    CD.Wait 1000

    CD.FindElementByClass(" css-1wy0on6").Click

    CD.Wait 500

    n = CD.FindElementByClass(" css-26l3qy-menu").FindElementByClass(" css-11unzgr").FindElementsByTag("div").Count
    Set ElementTable = CD.FindElementByClass(" css-26l3qy-menu").FindElementByClass(" css-11unzgr").FindElementsByTag("div")
    
        For i = 1 To n
            If InStr(ElementTable(i).Text, ShopFldArr(ShopPin, 5)) > 0 Then
                ElementTable(i).Click
                Exit For
            End If
        Next i

        CD.FindElementByXPath("//*[@id='left-tabs-example-tabpane-6']/button").Click '//*[@id="left-tabs-example-tabpane-6"]/button
        CD.Wait 3000
        
        Call XLPartsStatements(ShopName)
        Call UnZipFile.UnZipFiles(ShopName)
        
        n = 0
        Set ElementTable = Nothing
        ShopName = 0
    End If
    

Next ShopPin

Call UnZipFile.ZippedPdfRename

CD.Close

Application.Wait 2000


'Set CD = Nothing
Set UserInput = Nothing
Set PasswordID = Nothing
Set SubmitBtn = Nothing
Set web_table = Nothing
Set ElementTable = Nothing

End Sub

***********************************************

Sub XLPartsStatements(ShopName As String)

ClickedInvoices = 0

'SeleniumWebElements
Dim StatementTable As Selenium.WebElements
Dim web_table As Selenium.WebElement

'Strings/Long
Dim SearchInvDate As String
Dim n As Long, i As Long, t As Long
Dim Attempt As Integer

SearchInvDate = Format(Trans.Range("N1").value, "mm/dd/yyyy") 'Change Date here

CD.Get "https://rapid.xlparts.com/statements"

CD.Wait 2000

Attempt = 0
    Do
    Attempt = Attempt + 1
    If CD.IsElementPresent(By.Class("fixed-nav-wrapper")) Then
    CD.Wait 5000
    Exit Do
    End If
    
    If Attempt = 30 Then
    Attempt = 0
    CD.Refresh
    End If
    
    Loop

'[B]CRASHES HERE SOMETIMES BECAUSE THE iFRAME IS ON THE SITE BUT NOTHING ACTUALLY VISIBLE[/B]

CD.SwitchToFrame CD.FindElementByCss("iframe")


'Do While CD.FindElementById("poh_hi_ui_SearchPage_0-searchAndResults-searchPanel-btnSearch").IsDisplayed = True
CD.FindElementById("poh_hi_ui_SearchPage_0-searchAndResults-searchPanel-btnSearch").Click
'DoEvents
'Exit Do
'Loop

CD.Wait 2000


CD.FindElementByXPath("/html/body/div[1]/div[2]/div/div/div/div[3]/div/div[2]/div[1]/div/div[1]/div[1]/div/div/div/div/div[1]/div[2]/div/div/table/tbody/tr/th[7]").Click 'FINDBETTER WAY
CD.Wait 500
CD.FindElementByXPath("/html/body/div[1]/div[2]/div/div/div/div[3]/div/div[2]/div[1]/div/div[1]/div[1]/div/div/div/div/div[1]/div[2]/div/div/table/tbody/tr/th[7]").Click 'FINDBETTER WAY
CD.Wait 2000
t = CD.FindElementsByClass("dojoxGridContent").Count
Set StatementTable = CD.FindElementsByClass("dojoxGridContent")

For n = 1 To t
    If StatementTable(n).FindElementsByClass("dojoxGridRow").Count > 0 Then
        For i = 1 To StatementTable(n).FindElementsByClass("dojoxGridRow").Count 'dojoxGridRowTable
            
            Set web_table = StatementTable(n).FindElementsByClass("dojoxGridRow")(i).FindElementByClass("dojoxGridRowTable")
            
           ' InvoiceClicks web_table, SearchInvDate

            InvoiceClicks web_table, SearchInvDate
        Next i

    End If

Next n


If ClickedInvoices > 0 Then
CD.FindElementById("poh_hi_ui_SearchPage_0-searchAndResults-searchResults-summary-exportLink").Click
CD.Wait 500
CD.FindElementByXPath("//*[@id='uniqName_57_0']").Click  '//*[@id="poh_hi_HIActionToolbar_0-textLinksContainer"]/div[2]
CD.Wait 500
CD.FindElementByXPath("//*[@id='dijit_form_Select_2']/tbody/tr/td[2]/input").Click
CD.Wait 500
CD.FindElementByXPath("//*[@id='dijit_MenuItem_10_text']").Click
CD.Wait 500
CD.FindElementByXPath("//*[@id='poh_hi_HIActionToolbar_0-textLinksContainer']/div[2]/a").Click
CD.Wait 5000

Attempt = 0
    Do
    Attempt = Attempt + 1
    If fileExistsFullPath(Folders.Range("A2").value & "export.zip") Then
    Application.Wait 1000
        Name Folders.Range("A2").value & "export.zip" As Folders.Range("A2").value & ShopName & ".zip"
        Exit Do
    End If
    If Attempt = 30 Then
        Attempt = 0
    MsgBox ("Failed to Download file for " & ShopName)
    Exit Sub
    End If
    
    Loop


CD.FindElementByXPath("//*[@id='uniqName_57_0']").Click

CD.Wait 500
CD.FindElementByXPath("//*[@id='dijit_form_Select_2']/tbody/tr/td[2]/input").Click
CD.Wait 500
CD.FindElementByXPath("//*[@id='dijit_MenuItem_4_text']").Click
CD.Wait 500
CD.FindElementByXPath("//*[@id='poh_hi_HIActionToolbar_0-textLinksContainer']/div[2]/a").Click
CD.Wait 5000

Attempt = 0
    Do
    Attempt = Attempt + 1
    If fileExistsFullPath(Folders.Range("A2").value & "export.zip") Then
    Application.Wait 1000
        Name Folders.Range("A2").value & "export.zip" As Folders.Range("A2").value & ShopName & "pdf.zip"
        Exit Do
    End If
    If Attempt = 30 Then
        Attempt = 0
    MsgBox ("Failed to Download file for " & ShopName)
    Exit Sub
    End If
    
    Loop


End If


Set web_table = Nothing
Set StatementTable = Nothing

n = 0
i = 0
t = 0
Attempt = 0


End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top