VBA Selenium Chrome Unexpected Alert Error - excel

I am currently using Selenium, VBA Excel, and Chrome to scrape information from a site. Everything works fine until one of the values from my list is not available on the site, then I get a Run-time error '26' UnexpectedAlertOpenError. Error image.
I have added this line Chrome.SwitchToAlert(5).accept to handle the error which works when the value is not available. Unfortunately, adding that line returns a Run-Time error '27' NoAlertPresentError when the value from the list is available. Error image.
The error messages make sense, can't act on what is not there. I need a way to check if there is an alert and if so then quit chrome, else run the rest of the code. I have tried If Chrome.FindElementsByTag("tr") Is Nothing Then Chrome.SwitchToAlert(5).accept else and If Chrome.FindElementsByXPath("//*[contains(text(),'Details')]").Count > 0 Then and other things but nothing seems to work to address the error 26.

Well so based on the VBA selenium library I checked apparently there is no way of checking a count of the alerts already displayed, something we could do for browser windows by using:
driver.Windows.Count 'driver being the variable used to refer to an instance of Chromedriver
So the solution I am using is of wrapping the entire selenium code between error handlers:
On Error Goto Problem:
' Your code over here
''''''
''''''
On Error Goto 0
Exit Sub
Problem:
If InStr(1, Err.Description, "Alert") <> 0 Then
driver.SwitchToAlert.accept 'could also use: driver.SwitchToAlert.dismiss
Resume Next 'resumes code from the next line in code that threw error
End If
This way I believe whenever any sudden unexpected alert pops up, the error handler will accept/dismiss the alert and with 'Resume Next' ensure program moves on from the next line

Related

Compile through VBT giving errors

In the Excel VBA, when I give Compile VBAproject, it doesn't show any error and the compilation is successful.
But I was trying to achieve the same using VBA command like below,
Public Function CheckForCompilerErrors()
On Error GoTo errpt
....Excel.VBE.CommandBars.FindControl(id:=578).Execute
Exit Function
errpt:
MsgBox Err.Description
End Function
Err.Description shows 'Object variable or With block variable not set'.
Before executing this method, I made sure that Compile option is not disabled.
But when I do the same through Compile->VBAProject no issues. How to identify from where the error is thrown when compiled through VBA. It is not highlighting to any code. Just displays the error message.

Runtime Error with Workbook Auto Open + MsgBox as response for an user

I was trying to put MsgBox into my code. It should be shown only if call Table.auto_open doesn't work.
In my final document are few of those Call statements and i would like to get only that msgbox, if one or few of Call statements doesn't work.
For Example "Auto_open" will be changed to "auto_op" what naturally won't be possible, because in real sheet it is "Auto_open".
Or in another example that code from "Auto_open" is broken.
I need some help with that. It's seems to be simple, but I think it's not possible to put that "On Error GoTo" code in that place just like that, because Call doesn't give me a real error?
Can someone say me what I'm doing wrong? I tried already all combinations of that Error handling, nothing works.
Private Sub Workbook_Open()
On Error GoTo Error
Call Tabelle1.auto_open
Exit Sub
Error:
MsgBox "Failure"
Resume Next
End Sub
If you are calling a sub that does not exist you will get a Compile Error.
You can check for these errors by going to VBE>Debug>Compile VBAProject (or just try to run the macro)
Compile Errors, much like Syntax Errors, have to be handled before you can run a sub. Thus, these errors cannot be handled with code such as On Error GoTo EH or On Error Resume Next as these are only activated once the sub is actually running.
You can convince yourself of this by producing a common Compile error, or Syntax error, and trying to step through the code (F8). You will notice that the error occurs on your Sub [Name] () line, which indicates that you never actually entered your sub before the error occurred. Thus, you can intuitively see that your error handler will never actually be activated, resulting in an error message being displayed.
Once you have accounted for all Compile/Sytax Errors, you can check out this link, which will explain how you can handle Run Time Errors when you are calling other sub procedures from a sub.

Bypassing a website's Security Certificate warning using VBA

I access a website through VBA. This website has a security certificate.
Is there a way to get through this warning and go straight to the webpage.
Sub Certificate()
Dim IE As Object
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("Website URL")
While IE.ReadyState <> 4
DoEvents
Wend
{Application.Wait (Now + TimeValue("0:00:05"))}--Optional
'This is the security certificates webpage HTML
'This clicks the not recommended but continue to website button
IE.document.getElementsByName("overridelink").Item.Click
End With
End Sub
When I execute this code this line:
IE.document.getElementsByName("overridelink").Item.Click
gives me a
Runtime error '91'
This code works when the security certificate warning pops up however it doesn't always pop up and goes straight to the website, giving me that error.
I've gone into internet options and unchecked everything that would cause that warning as well as adding it to trusted site.
CAUTION! Use sparingly!
If the only time you get an error with the line IE.document.getElementsByName("overridelink").Item.Click is because you don't need it, you can use this:
On Error Resume Next
IE.document.getElementsByName("overridelink").Item.Click
On Error Goto 0
instead of:
IE.document.getElementsByName("overridelink").Item.Click
CAUTION! Read this:
Make sure to include the On Error Goto 0 line directly after the section you want the error ignored on, as that line resets the error-behavior to default. Otherwise, the On Error Resume Next will mask any and all other errors in the code following it. This can cause HUGE issues with code giving unexpected results for no apparent reason and/or attempts to troubleshoot problematic code.
Another issue: if the error-skipping enclosed code goes wrong for an unexpected reason, you won't have any way of catching this unless you set-up a more elaborate error catching section in your code.

Err.Number not populating for 'Excel Ran Out of Resources' error

I've developed a program using Excel VBA which occasionally causes an 'Excel Ran Out of Resources' error.
Closing the file, reopening, and rerunning the macro always fixes whatever issue created the error. I know that preventing the error in the first place is best practice, but am resigned to believe that it's unavoidable.
So, I would like to alert the user of the error, instead of Excel doing it, and perform some actions once the error has been detected. The problem is that I can't get VBA to recognize the error using the On Error GoTo ErrorHandler routine or the Err.Number property. I never get to the msgbox below:
My code is as follows:
Sub test()
On Error GoTo ErrorHandler
Calculate
ProcedureDone:
Exit Sub
ErrorHandler:
MsgBox "Error", vbOKOnly, "Oops"
Resume ProcedureDone
End Sub
Any insight would be fantastic since I've been searching for several days and haven't been able to find a work around.
I just happened to run across another issue that sounds like yours.
The point from the other thread is that Excel does not treat these application method results as VBA errors. Rather, they are Excel alerts, and they can be suppressed but not trapped in VBA as errors.
The way I interpret this is that when you execute certain application methods from VBA, it does not raise errors that VBA can trap. Rather, Excel interacts with the user as if the user had issued a GUI command. On the other hand, if an application method is designed to interact with VBA (e.g., if it returns a value), then VBA might be able to handle its errors.
This is distinct from the way VBA handles worksheet functions rather than application methods. VBA can intercept errors raised by worksheet functions, as noted in "Error Handling With Worksheet Functions" here.
I realize this does not solve your problem, but it gives you an idea of why.

Alternate message when VBA can't open file

I have a VBA script that when the user executes it, it opens and writes new data to another file on a Network share drive. Occasionally the script can't access this file to write new data to it because someone else is simultaneously trying to open it (via read only through a VBA script that they have).
This particular file opening error is not caught in the error handling code and a box comes up and asks the user to End or Debug. Is there a way to bring up an alternate popup box that says something like "Could not process request. Please wait a few seconds and try again."
yes. put in error handling around the file opening code. like this:
on error goto FileError
'file opening code
on error goto 0
exit sub
FileError:
msgbox("Could not process request. Please wait a few seconds and try again")
basically, the OnError tells everything after that statement to do whatever you specify in the event of an error. in this case I've put goto FileError so if an error is encountered anywhere after the onError processing will stop and go to the line marked FileError.
then the OnError goto 0 disables that error handle. that way if other code generates an error it wont be handled by the FileError handle. the other option for onerror is OnError Resume Next which makes the program skip over code that generates an error. so it wont throw the error, and it will just continue processing.
Note the: Exit Sub before the FileError label, that way the code wont go into the FileError label unless it is sent there by an error being generated.

Resources