Also, tried to use Window Analysis tool for System Events to see which button is pressed (but for some reason can't activate it when I am on OpenConnect)
So if anyone can answer both questions
how to simulate pressing Connect
how to use Window Analysis tool so that I can fish nextime
For things like this where other options aren't working I usually fall back on GUI AppleScripting. UI Browser will give you the element's information which you can use to have System Events click on it.
I don't know if you've solved your problem or if my suggestion will help your situation, but when I look at your code I see some risky assumptions that you are making. I run into situations similar to yours all the time, with programs that behave oddly, and here's how I would try to address the problem. Instead of running the action to press the button as you do, I would first write code to wait until the button appears, and then press (repeatedly, with a short pause for safety) until it disappears. Something like this:
This approach is a safer way to press a button, but it doesn't even take into account some situations like handling timeouts. So when I write code as simple as pressing a button, and I want to do it reliably, sometimes the code to press the button can be a full page in size.
In the future I plan to use subroutines to press buttons like this. My subroutine would probably look something like this:
The error message could perhaps be sent as an SMS message to my Apple Watch, if I wanted to see my error handling messages when I'm away from home.
I didn't upload this code because I didn't test it, and because it's too trivial to upload.
I don't have that VPN so don't know how it works but does it not work to tab to the Connect button and press the spacebar to activate it? A lot of apps work that way.