Help with error handling, I need to retry the step

BalloutMoe

Board Regular
Joined
Jun 4, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
Hello below is a code I use however sometimes I encounter problems with certain lines that I need them to repeat that step then continue. Looking for some help here, thank you

VBA Code:
Sub GetDatafromEdge()
Dim findby As New Selenium.By
Dim driver As New WebDriver
'Set driver = New WebDriver
Dim keys As New Selenium.keys

driver.Start "edge", ""
'Website I access here

driver.FindElementByName("ctl00$cpMain$logMain$UserName").SendKeys "****"
driver.FindElementByName("ctl00$cpMain$logMain$Password").SendKeys "*****"
driver.FindElementByName("ctl00$cpMain$logMain$LoginButton").Click

Dim LastRow As Long
LastRow = Range("F" & Rows.Count).End(xlUp).Row
Dim filename As String
With driver
       'Open Edge and navigate to website
    For Each Url In ActiveWorkbook.ActiveSheet.Range("F2:F" & LastRow)
            .Get Url.Value
            filename = Url.Offset(0, -5).Value & " " & Url.Offset(0, -1).Value

            driver.Wait 3000
        [COLOR=rgb(97, 189, 109)]'I am having issues here sometimes it does not find the element and it kicks me out with the error, I would like to add an error handling in case it does not to repeat this step until it finds it.[/COLOR]
            driver.FindElementById("VisibleReportContentrptMain_ctl13").FindElementByTag("div").WaitDisplayed
            driver.Wait 3000

   [COLOR=rgb(97, 189, 109)]'Also same issue here[/COLOR]
                driver.FindElementByCss(".ToolbarExport.WidgetSet").Click
                driver.Wait 1000
                'Application.Wait Now + TimeValue("00:00:2")
   [COLOR=rgb(97, 189, 109)]'Also same issue here[/COLOR]
                driver.FindElementByXPath("//a[@title='PDF']").Click
                driver.Wait 2000
                'Application.Wait Now + TimeValue("00:00:1")
   [COLOR=rgb(97, 189, 109)]'Also same issue here[/COLOR]
              Name "C:\Users\Downloads\rptNG2COSInvoice.pdf" As "C:\Users\Downloads\" & filename & ".pdf"
                
            Next
            
    End With
     driver.Wait 2000
End Sub

I added a bunch of driver.wait lines just to help, for the most part the code works but occasionally the site takes a while to load and it throws errors or the internet is slow. Please if you can help me out I would greatly appreciate it. Thank you
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi,
try adding following lines shown in BOLD & see if helps

Rich (BB code):
On Error GoTo myerror
  
    With driver
        'Open Edge and navigate to website
        For Each Url In ActiveWorkbook.ActiveSheet.Range("F2:F" & LastRow)
            .Get Url.Value
            filename = Url.Offset(0, -5).Value & " " & Url.Offset(0, -1).Value
            
            driver.Wait 3000
            'I am having issues here sometimes it does not find the element and it kicks me out with the error, I would like to add an error handling in case it does not to repeat this step until it finds it.
            driver.FindElementById("VisibleReportContentrptMain_ctl13").FindElementByTag("div").WaitDisplayed
            driver.Wait 3000
           
            'Also same issue here
            driver.FindElementByCss(".ToolbarExport.WidgetSet").Click
            driver.Wait 1000
            'Application.Wait Now + TimeValue("00:00:2")
            'Also same issue here
            'PDF']").Click
            driver.Wait 2000
            'Application.Wait Now + TimeValue("00:00:1")
            'Also same issue here[/COLOR]
            Name "C:\Users\Downloads\rptNG2COSInvoice.pdf" As "C:\Users\Downloads\" & filename & ".pdf"[/COLOR][/COLOR][/COLOR]

myerror:
   If Err <> 0 Then Resume Next
        Next

Dave
 
Upvote 0
Hi,
try adding following lines shown in BOLD & see if helps

Rich (BB code):
On Error GoTo myerror
 
    With driver
        'Open Edge and navigate to website
        For Each Url In ActiveWorkbook.ActiveSheet.Range("F2:F" & LastRow)
            .Get Url.Value
            filename = Url.Offset(0, -5).Value & " " & Url.Offset(0, -1).Value
            
            driver.Wait 3000
            'I am having issues here sometimes it does not find the element and it kicks me out with the error, I would like to add an error handling in case it does not to repeat this step until it finds it.
            driver.FindElementById("VisibleReportContentrptMain_ctl13").FindElementByTag("div").WaitDisplayed
            driver.Wait 3000
           
            'Also same issue here
            driver.FindElementByCss(".ToolbarExport.WidgetSet").Click
            driver.Wait 1000
            'Application.Wait Now + TimeValue("00:00:2")
            'Also same issue here
            'PDF']").Click
            driver.Wait 2000
            'Application.Wait Now + TimeValue("00:00:1")
            'Also same issue here[/COLOR]
            Name "C:\Users\Downloads\rptNG2COSInvoice.pdf" As "C:\Users\Downloads\" & filename & ".pdf"[/COLOR][/COLOR][/COLOR]

myerror:
   If Err <> 0 Then Resume Next
        Next

Dave
Thank you for your reply but the following code opens each URL one by one and download the pdf from the website, so if I skip the error and go to resume next does that open the next link in F column or the same one again? sometime it could be up to 300 URLs and I need each of them downloaded.
 
Upvote 0
If you just want the code to move to the next line if an error occurs, then ignore my last post & just add the line shown in BOLD.

Rich (BB code):
On Error Resume Next
   
   
With driver
        'Open Edge and navigate to website
        For Each Url In ActiveWorkbook.ActiveSheet.Range("F2:F" & LastRow)


'rest of code


Dave
 
Upvote 0
Hi,
If you want to keep going back to a line errors until success then you can try this update

Rich (BB code):
    Dim Attempt         As Integer
    Const MaxAttempts   As Integer = 3
    
    On Error GoTo myerror
   
    With driver
        'Open Edge and navigate to website
        For Each Url In ActiveWorkbook.ActiveSheet.Range("F2:F" & LastRow)
            .Get Url.Value
            filename = Url.Offset(0, -5).Value & " " & Url.Offset(0, -1).Value
            
            driver.Wait 3000
            'I am having issues here sometimes it does not find the element and it kicks me out with the error, I would like to add an error handling in case it does not to repeat this step until it finds it.
            driver.FindElementById("VisibleReportContentrptMain_ctl13").FindElementByTag("div").WaitDisplayed
            driver.Wait 3000
           
           
            'Also same issue here[/COLOR]
            driver.FindElementByCss(".ToolbarExport.WidgetSet").Click
            driver.Wait 1000
            'Application.Wait Now + TimeValue("00:00:2")
            'Also same issue here[/COLOR]
            'PDF']").Click
            driver.Wait 2000
            'Application.Wait Now + TimeValue("00:00:1")
            'Also same issue here[/COLOR]
            Name "C:\Users\Downloads\rptNG2COSInvoice.pdf" As "C:\Users\Downloads\" & filename & ".pdf"[/COLOR]
myerror:
    If Err <> 0 Then
        Attempt = Attempt + 1
        If Attempt > MaxAttempts Then Attempt = 0
        If Attempt = 0 Then Resume NextURL Else Resume
    Else
        Attempt = 0
    End If
    
NextURL:
        Next

Add all lines code shown in BOLD. I have included a MaxAttempts constant which I have set at 3 to avoid endless loop. You can change this to number of attempts as required.

This suggestion is UNTESTED & you should make a BACKUP before attempting to use it in your project.

Dave
 
Upvote 0
Solution
Hi,
If you want to keep going back to a line errors until success then you can try this update

Rich (BB code):
    Dim Attempt         As Integer
    Const MaxAttempts   As Integer = 3
    
    On Error GoTo myerror
  
    With driver
        'Open Edge and navigate to website
        For Each Url In ActiveWorkbook.ActiveSheet.Range("F2:F" & LastRow)
            .Get Url.Value
            filename = Url.Offset(0, -5).Value & " " & Url.Offset(0, -1).Value
            
            driver.Wait 3000
            'I am having issues here sometimes it does not find the element and it kicks me out with the error, I would like to add an error handling in case it does not to repeat this step until it finds it.
            driver.FindElementById("VisibleReportContentrptMain_ctl13").FindElementByTag("div").WaitDisplayed
            driver.Wait 3000
          
          
            'Also same issue here[/COLOR]
            driver.FindElementByCss(".ToolbarExport.WidgetSet").Click
            driver.Wait 1000
            'Application.Wait Now + TimeValue("00:00:2")
            'Also same issue here[/COLOR]
            'PDF']").Click
            driver.Wait 2000
            'Application.Wait Now + TimeValue("00:00:1")
            'Also same issue here[/COLOR]
            Name "C:\Users\Downloads\rptNG2COSInvoice.pdf" As "C:\Users\Downloads\" & filename & ".pdf"[/COLOR]
myerror:
    If Err <> 0 Then
        Attempt = Attempt + 1
        If Attempt > MaxAttempts Then Attempt = 0
        If Attempt = 0 Then Resume NextURL Else Resume
    Else
        Attempt = 0
    End If
    
NextURL:
        Next

Add all lines code shown in BOLD. I have included a MaxAttempts constant which I have set at 3 to avoid endless loop. You can change this to number of attempts as required.

This suggestion is UNTESTED & you should make a BACKUP before attempting to use it in your project.

Dave
Thank you this worked perfectly. I appreciate it
 
Upvote 0

Forum statistics

Threads
1,223,883
Messages
6,175,167
Members
452,615
Latest member
bogeys2birdies

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