0

I'm putting errors in Try and Except, but I'm having trouble with 2 types of errors. I'll give you an example of what I need. This except with Valure Error works fine, because I know the error is called ValueError, so:

#ONLY EXAMPLE
except ValueError:
    return "FAILED"

EXCEPT ERROR N.1 The question I ask you is: how should I write for the following error? It consists of the wrong name of an html class when scraping. I wrote selenium.common.exceptions.NoSuchElementException, but the error search does not work (the script does not open at all)

except selenium.common.exceptions.NoSuchElementException:
    return "FAILED"

EXCEPT ERROR N.2 Same thing for the database insert error. I want to make sure that if 0 records are saved in the database then I have an error. I tried to write like this, but it's not good:

except  records_added_Results = records_added_Results = + 0:
    return "FAILED"

I wrote this because I have this code for the insertion in the database. It works correctly, I am writing it just to make you understand:

    con = sqlite3.connect('/home/mypc/Scrivania/folder/Database.db')
    cursor = con.cursor()
    records_added_Results = 0

    Values = ((SerieA_text,), (SerieB_text,)
    sqlite_insert_query = 'INSERT INTO ARCHIVIO_Campionati (Nome_Campionato) VALUES (?);'
    count = cursor.executemany(sqlite_insert_query, Values) 
    con.commit()

    print("Record inserted successfully ", cursor.rowcount)
    records_added_Results = records_added_Results + 1
    cursor.close()

How should I write? Thank you

  • What have you imported? If you have `from selenium.common.exceptions import NoSuchElementException` you can simply use `except NoSuchElementException` – not_speshal Jul 12 '21 at 13:07
  • @not_speshal I imported two things: from selenium import webdriver and then from selenium.webdriver.firefox.firefox_profile import FirefoxProfile. So how should I write? Thank you – Frederick Man Jul 12 '21 at 13:09
  • Which part of your code causes the `NoSuchElementException`? – not_speshal Jul 12 '21 at 13:11
  • @not_speshal When with driver.get ("link"), I search for a class in a site to scrape it with SerieC_B = driver.find_element_by_class_name ("teamHeader__name"). I want to make sure that: if the class has a different name from the one I wrote (for example changed by the site over time/years), then I get the error selenium.common.exceptions.NoSuchElementException – Frederick Man Jul 12 '21 at 13:14

1 Answers1

1

Import the exception before you try to catch it. Add this line to your imports:

from selenium.common.exceptions import NoSuchElementException

Then you can simply do:

try:
    #code that could raise the NoSuchElementException
except NoSuchElementException:
    #exception handling

For the other exception, it looks like you're trying to raise an exception. Consider something like this:

if records_added_Results == 0:
    raise ValueError("error message")
not_speshal
  • 22,093
  • 2
  • 15
  • 30
  • I thank you for the answer. Regarding the second exception, how can I put the if inside except? Just write except next to if? So: except if records_added_Results == 0:? Or is there another way? Thank you – Frederick Man Jul 12 '21 at 13:20
  • Is it correct if I write like this? except records_added_Results == 0: return "error message" ? (I need to display the error message in a graphic window made with Tkinter ) – Frederick Man Jul 12 '21 at 13:28
  • I don't understanding why you need a `try`/`except` block. You're raising an exception. `if`/`raise` should suffice. – not_speshal Jul 12 '21 at 13:32
  • Read this other question of mine (not yet completed): https://stackoverflow.com/questions/68333551/start-a-function-of-a-py-file-in-another-py-file-replace-icons- and-display-exce. That's why I need the exception. I am making an error check with graphics window and 2 different py files. I would like to do that if there is the NoSuchElementException error or also the records_added_Results == 0 error, in another file.py with graphics window ... the red error icon is displayed – Frederick Man Jul 12 '21 at 13:39
  • And how does using `raise` stop you from doing that? It's still forcing an exception to be raised if your condition is satisfied. If that doesn't work, see [custom exceptions](https://stackoverflow.com/questions/1319615/proper-way-to-declare-custom-exceptions-in-modern-python). – not_speshal Jul 12 '21 at 13:49
  • Because in the other py file, that is the one with the graphics window, a code has been written that calls ONLY the except. I don't know if it includes ifs. Will your if, without except, be called anyway? Look at the code they suggested here - it's related to this one you just wrote me. https://stackoverflow.com/questions/68333551/start-a-function-of-a-py-file-in-another-py-file-replace-icons-and-display-exce – Frederick Man Jul 12 '21 at 13:54
  • in the other py file, that is the one with the graphics window, a code has been written that calls ONLY the except. – Frederick Man Jul 12 '21 at 13:57
  • It's really not that difficult. You can simply add the `if`/`raise` before `print("Record inserted successfully ", cursor.rowcount)` ? – not_speshal Jul 12 '21 at 13:58
  • My curiosity, just curiosity. If I write: expect records_added_Results == 0: return "message", does it work? – Frederick Man Jul 12 '21 at 14:03
  • Because I'm on another pc and I don't have the files with me. I'll try tonight when I stay at my house. I was just wondering theoretically with Python was it possible to write this except this way: expect records_added_Results == 0: return "message" – Frederick Man Jul 12 '21 at 14:05