#๐Ÿ”’ Except statement not working?

36 messages ยท Page 1 of 1 (latest)

stray coral
#

Hello!
This is my nested try code block:

        #Searches for the "Email is not valid" popup
        time.sleep(0.3)
        driver.find_element(by="xpath", value="/html/body/div/form[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[3]/div/div/div/div[2]/div[1]/div")
    except:    THIS SHOULD EXECUTE
        try:
            #Email is Valid, writes in Password
            driver.find_element(by="id", value="passwordEntry").send_keys(Password + Keys.ENTER)
        except:
            driver.close()
            return "Invalid"
        else:
            try:
                #Password is Valid, searches for the "Yes" button, meaning no 2FA
                driver.find_element(by="xpath", value="/html/body/div/div/div/div/div/div[1]/div/div/div/div/form")
            except:
                    try:
                        driver.find_element(by="xpath", value="/html/body/div[1]/div/div/div[2]/div/div[1]/div[2]/div/div[2]/div/div/form/div/div[6]/div/div/input")
                    except:
                        #Doesn't find the "NEXT" or "YES" button but a 2fa screen instead
                        return "2FA"
                    else:
                        #Finds a "NEXT" button
                        with open("cookie.pkl", "wb") as file:
                            pickle.dump(cookies, file)
                        return "Valid"
                    finally:
                        driver.close()
            else:
                #Finds the "Yes" button, account is Valid.
                with open("cookie.pkl", "wb") as file:
                    pickle.dump(cookies, file)
                return "Valid"
            finally:
                driver.close()
    else:
        driver.quit()
        return "Invalid"```
wanton wolfBOT
#

@stray coral

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

stray coral
#

Why doesn't the except statement execute?

This is my error Traceback (most recent call last): File "C:\Users\(USER)\filetesting\login\main.py", line 40, in main driver.find_element(by="xpath", value="/html/body/div/form[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[3]/div/div/div/div[2]/div[1]/div")

gray shore
#

what's thee full output?

#

the above is truncated

stray coral
# gray shore what's thee full output?

I believe that this is it: Traceback (most recent call last): File "C:\Users\jirir_r7cm2uq\filetesting\login\main.py", line 40, in main driver.find_element(by="xpath", value="/html/body/div/form[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[3]/div/div/div/div[2]/div[1]/div") ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 922, in find_element return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"] ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 454, in execute self.error_handler.check_response(response) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^ File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/form[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[3]/div/div/div/div[2]/div[1]/div"} (Session info: chrome=139.0.7258.67); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#nosuchelementexception Stacktrace:

gray shore
#

that's still no tall, see it's cutoff at the bottom

stray coral
#

Okay, ill paste it in a pastebin, doesn't fit here

stray coral
#

Try this! Thank you ๐Ÿ™‚

#

Or I could send it seperatly in a lot of messages if you prefer that

#
Clear output.txt? (Yes/No) Yes
input.txt found!
Traceback (most recent call last):
  File "C:\Users\jirir_r7cm2uq\filetesting\login\main.py", line 40, in main
    driver.find_element(by="xpath", value="/html/body/div/form[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[3]/div/div/div/div[2]/div[1]/div")
    ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 922, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 454, in execute
    self.error_handler.check_response(response)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div/form[1]/div/div/div[2]/div[1]/div/div/div/div/div[1]/div[3]/div/div/div/div[2]/div[1]/div"}
  (Session info: chrome=139.0.7258.67); For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#nosuchelementexception
Stacktrace:```
#
    GetHandleVerifier [0x0x7ff612ba6bb0+79712]
    (No symbol) [0x0x7ff61293c0ea]
    (No symbol) [0x0x7ff612992f56]
    (No symbol) [0x0x7ff61299320c]
    (No symbol) [0x0x7ff6129e65b7]
    (No symbol) [0x0x7ff6129bb17f]
    (No symbol) [0x0x7ff6129e33d0]
    (No symbol) [0x0x7ff6129baf13]
    (No symbol) [0x0x7ff612984151]
    (No symbol) [0x0x7ff612984ee3]
    GetHandleVerifier [0x0x7ff612e6686d+2962461]
    GetHandleVerifier [0x0x7ff612e60b8d+2938685]
    GetHandleVerifier [0x0x7ff612e7f74d+3064573]
    GetHandleVerifier [0x0x7ff612bc0c9e+186446]
    GetHandleVerifier [0x0x7ff612bc8a6f+218655]
    GetHandleVerifier [0x0x7ff612baf944+115956]
    GetHandleVerifier [0x0x7ff612bafaf9+116393]
    GetHandleVerifier [0x0x7ff612b95f28+10968]
    BaseThreadInitThunk [0x0x7ffdc08ee8d7+23]
    RtlUserThreadStart [0x0x7ffdc221c34c+44]

#

Traceback (most recent call last):
  File "C:\Users\jirir_r7cm2uq\filetesting\login\main.py", line 99, in <module>
    output = main(separated[0], separated[1])
  File "C:\Users\jirir_r7cm2uq\filetesting\login\main.py", line 71, in main
    driver.close()
    ~~~~~~~~~~~~^^
  File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 602, in close
    self.execute(Command.CLOSE)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 454, in execute
    self.error_handler.check_response(response)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "C:\Users\jirir_r7cm2uq\filetesting\login\.venv\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 232, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSessionIdException: Message: invalid session id; For documentation on this error, please visit: https://www.selenium.dev/documentation/webdriver/troubleshooting/errors#invalidsessionidexception
Stacktrace:
    GetHandleVerifier [0x0x7ff612ba6b55+79621]
    GetHandleVerifier [0x0x7ff612ba6bb0+79712]
    (No symbol) [0x0x7ff61293bf1c]
    (No symbol) [0x0x7ff61298327f]
    (No symbol) [0x0x7ff6129bb002]
    (No symbol) [0x0x7ff6129b5b23]
    (No symbol) [0x0x7ff6129b4be9]
    (No symbol) [0x0x7ff612906fd5]
    GetHandleVerifier [0x0x7ff612e6686d+2962461]
    GetHandleVerifier [0x0x7ff612e60b8d+2938685]
    GetHandleVerifier [0x0x7ff612e7f74d+3064573]
    GetHandleVerifier [0x0x7ff612bc0c9e+186446]
    GetHandleVerifier [0x0x7ff612bc8a6f+218655]
    (No symbol) [0x0x7ff612905fd1]
    GetHandleVerifier [0x0x7ff612f7e458+4108296]
    BaseThreadInitThunk [0x0x7ffdc08ee8d7+23]
    RtlUserThreadStart [0x0x7ffdc221c34c+44]


Process finished with exit code 1
#

This is everything!

gray shore
#

you've also not shared all the code, includnig the section throwing the exception:

  File "C:\Users\jirir_r7cm2uq\filetesting\login\main.py", line 99, in <module>
    output = main(separated[0], separated[1])
  File "C:\Users\jirir_r7cm2uq\filetesting\login\main.py", line 71, in main
    driver.close()```
#

I 100% gaurantee except works, you're probably just looking at the wrong sectioion of code becuase you're looking at the line at the top of the first traceback

stray coral
gray shore
#

right, it calls out lines 99 and 71

stray coral
#

But isn't that During handling of the above exception, another exception occurred:

#

and those exceptions occured because of the first exception

gray shore
#

right, in that exception block arouound line 40, another exception is happening

#

it wasn't "because" of the first exception, it was while handling it

stray coral
#

Oh okay, my apologies!

gray shore
#

no need to appologize

stray coral
gray shore
#

whatever wroks

stray coral
wanton wolfBOT
gray shore
#

it's the line 71 driver.close() that's the root issue

stray coral
#

It was!!! Thank you so much โค๏ธ

gray shore
#

sure thing!

stray coral
#

Also, is there a way to make the nested try: methods more readable? Because now I have try: everywhere and it's a bit complicated to debug

wanton wolfBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.