Restart script on error - python

so I have this script in selenium, that it sometimes crashes for various reasons. Sometimes it fails to click a button, gets confused, gets messed up and displays an error.
How can I command the script, so whenever it crashes, to re-run the script from the beginning again? I've heard about try and except functions but I'm not sure how to use them.
Any help is appreciated! :)
[Using Python 2.7 with Selenium Webdriver]

generic answer to retry on any exception:
while True:
try:
# run your selenium code which sometimes breaks
pass
except Exception as e:
print("something went wrong: "+repr(e))
you may try to refine the exception type to avoid retrying say, because of a python error like ValueError or IOError. Check the exception type and change Exception by the qualified type.

Related

How to catch a playwright freeze?

I have a script in Playwright 1.23.1 with Python 3.10.2 running in Ubuntu LTS 20.4 to extract data from a page, the scripts run for about an hour because it is a lot of data. The problem is that sometimes the execution freezes and I can't catch it and I have to give the SIGINT sign (CTRL + C) to stop the execution. I don't know if it is a problem of Playwright or it is of the page that it is browsing. This page is really not that good, it's slow and have some problems in general.
My code is something like this:
from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
from playwright.sync_api import Error as PlaywrightError
with sync_playwright() as spw:
# connect to the page
browser, page = connect(spw)
# sign in with credentials
sign_in(username, page)
data_code = 1
while True:
try:
# Extract the data from the page using playwright methods
# including select_option, frame_locator, evaluate, click, and reload
end_signal = extract_data(page, data_code)
if end_signal:
break
except (TimeoutError, PlaywrightTimeoutError, PlaywrightError) as e:
print(f"Error is: {e}")
page.reload(timeout=90000, wait_until='domcontentloaded')
data_code += 1
print("Exit cycle")
That's the basic structure of my code, the problem is that sometimes when trying to use click, select_option, or reloading the execution freezes. There are some prints inside extract_data but they don't appear, and sometimes print(f"Error is: {e}") does not print, so it does reach it, therefore, my conclusion is that the execution is freezed.
I have researched a little about Playwright debug, but in general I can't find the problem, and this error happens sometimes not always, so I can't repliacte it for a proper debug with playwights. The logs at least let me identify where it freezes, but nothing else so far.
Hope someone can help me, thanks.

Clarification regarding a statement on exceptions

My textbook says :
"In Python, exceptions are errors that get triggered automatically. "
What does it mean? I had thought about this statement for sometime but unable to understand it.
(P.S. I am a beginner in the computer field . Please give a understandable answer for me)
"In Python, exceptions are errors that get triggered automatically. "
That's true, an even more correct definition would be the following.
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.
You should imagine your interpreter (I guess you're using CPython) executing line after line until an event happens.
That event can be an error, that is passed to your script as an Exception (or sublcass) object.
"[...] that get triggered automatically"
That's not completely true, you can throw (or better saying raise) an exception manually.
raise Exception("Error occurred!")

python exception not reported

Python version 2.7.13
I was debugging an issue in my program. I use pycharm debug to find that there is a key error, dict try to get a key not exist in it, but there is no traceback reported. In my program, we use sys.excepthook = <my_callback_func> to catch the uncaught trace, it usually works, but in my issue, it didn't work, the code didn't enter <my_callback_func>.
Maybe there is some other try catch code which catch this exception but I can't find it, because the program is quite large and complex.
So here is my question, is there a way to find which try catch code capture this exception, I use pycharm debug tools, but when I step debug it, the code never go to the catch code. or maybe there is some other reason that the sys.excepthook not working.

How to hard suspend or pause a python script after it runs so it doesn’t force close upon completion?

Hi so I’m working on a python script that involves a loop function, so far the loop function process is failing for some reason(although I kinda know why) but the problem I’ve got os.system(‘pause’) and also input(“prompt:”) at end of the code in order to pause all activity so I can read the error messages prior to script completion and termination but the script still shuts down, I need a way to HARD pause it or freeze before the window closes abruptly. Need help and any further insight.
Ps. Let me know if you need any more info to better describe this problem.
I assume you are just 'double clicking' the icon on Window Explorer. This has the disadvantage which you are encountering here in that the shell (terminal window) closes when the process finishes so you can't tell what went wrong if it terminated due to an error.
A better method would be to use the command prompt. If you are not familiar with this, there are many tutorials online.
The reason this will help with your problem is that, once navigating to the script's containing directory, you can use python your_script.py (assuming python is in your path environmental variable) to run the script within the same window.
Then, even if it fails, you can read the error messages as you will only be returned to the command line.
An alternative hacky method would be to create a script called something like run_pythons.py which will use the subprocess module to call your actual script in the same window, and then (no matter how it terminates), wait for your input before terminating itself so that you can read the error messages.
So something like:
import subprocess
subprocess.call(('python', input('enter script name: ')))
input('press ENTER to kill me')
I needed something like this at one point. I had a wrapper that loaded a bunch of modules and data and then waited for a prompt to run something. If I had a stupid mistake in a module, it would quit, and that time that it spent loading all that data into memory would be wasted, which was >1min. For me, I wanted a way to keep that data in memory even if I had an error in a module so that I could edit the module and rerun the script.
To do this:
while True:
update = raw_input("Paused. Enter = start, 'your input' = update params, C-C = exit")
if update:
update = update.split()
#unrelevant stuff used to parse my update
#custom thing to reload all my modules
fullReload()
try:
#my main script that needed all those modules and data loaded
model_starter.main(stuff, stuff2)
except Exception as e:
print(e)
traceback.print_exc()
continue
except KeyboardInterrupt:
print("I think you hit C-C. Do it again to exit.")
continue
except:
print("OSERROR? sys.exit()? who knows. C-C to exit.")
continue
This kept all the data loaded that I grabbed from before my while loop started, and prevented exiting on errors. It also meant that I could still ctrl+c to quit, I just had to do it from this wrapper instead of once it got to the main script.
Is this somewhat what you're looking for?
The answer is basically, you have to catch all your exceptions and have a method to restart your loop once you figured out and fixed the issue.

Handle assertion dialog box with python subprocess

I am using python to create a sub process to check and see that no assertions occur.
I want to catch the error output along with the return code. That works fine, but the problem I run into is that when it runs into the assertion it gives me a dialog box that just hangs there. I have to then click the assertion box before I retrieve any information. Is there a way to make it not pop up and continue with the program or to send a message to close the window?
This is a problem since this is an automation service.
import subprocess
pipe = subprocess.Popen('test2.exe', shell=True, stderr=subprocess.PIPE)
for line in pipe.stderr:
print line
The executable is compiled from c++ code and has an assertion that will fail for testing purposes.
There's not really an easy solution in general, since a program could in theory create any number of windows waiting for user input. If you have the source code for the inferior process, the easiest thing to do would be to modify it to call _set_abort_behavior(0, _CALL_REPORTFAULT) to disable the message box.
If you don't have the source code, it's going to be much, much tougher. You could probably write a big hack that did something like attaching a debugger to the inferior process and setting a breakpoint on the call to abort(). If that breakpoint gets hit, kill the process and return an appropriate error status. But that's an extreme non-trivial kludge.
As I mentioned in the comments, pop-ups for assertions isn't a normal thing in Python. If you can modify the code running in a subprocess, you might be able to capture the assertion and handle it yourself, rather than letting the environment handle it with a popup.
import sys
try:
code that raises the assertion
catch AssertionError, e:
sys.stderr.write("Assertion failed: " + str(e))
sys.exit(1)
If it's looking for assertions in particular, this should work, because it will capture the assertion, print an error message and then raise a simple SystemExit exception instead.

Categories