Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python selenium error handling

I have a python selenium script that runs through a loop like this...

for i, refcode in enumerate(refcode_list):

    try:
        source_checkrefcode()
    except TimeoutException:
        pass
        csvWriter.writerow([refcode, 'error', timestamp])

If there is a problem during the source_checkrefcode then the script crashes with an error.

How can I add error handling to this loop so that it just moves to the next item instead of crashing?


1 Answers

You can add a check over exception message, below is the sample code for your understanding.

for i, refcode in enumerate(refcode_list):
    try:
        source_checkrefcode()
    except Exception as e:
        if 'particular message' in str(e):
            # Do the following
            # if you don't want to stop for loop then just continue
            continue
like image 142
Hassan Mehmood Avatar answered Sep 15 '26 15:09

Hassan Mehmood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!