0

I have the following code and for some reason it doesn't quit the browser when the two elif statements are triggered OR go to the except part of the code.

The goal is to return False in every case where the "Solved" attribute is not found. I have tried removing the elif parts so that it would go to except part in all of the other cases but it still doesn't work.

while True:
        try:
            sleep(10)
            status = browser.find_element(By.CLASS_NAME, 'status')
            if status.get_attribute("innerHTML") == "Solved":
                break
            elif status.get_attribute("innerHTML") == "Unknown error, watch console":
                browser.quit()
                print("Unknown error - programm ootab 3 minutit...\n")
                sleep(180)
                return False
            elif status.get_attribute("innerHTML") == "Outdated, should be solved again":
                browser.quit()
                print("Captcha outdated - programm ootab 3 minutit...\n")
                sleep(180)
                return False

        except:
            print('Captcha fked up for some reason \n')
            browser.quit()
            return False
Karuvägistaja
  • 293
  • 1
  • 8
  • 17
  • 1
    What error do you expect thrown by what function that would cause you to go to the `except` clause? – matszwecja Mar 14 '22 at 15:51
  • if the "solved" is not found the it would go to except or if the two other elif statements are found to be present then return false. – Karuvägistaja Mar 14 '22 at 15:54
  • 1
    ...that's not what `except` does. `except` lets you handle an exception that was thrown in the `try` block. Are you looking for `else`? – ChrisGPT was on strike Mar 14 '22 at 15:55
  • 1
    Based on the documentation of Selenium, `get_attribute` will not raise any error if the attribute is not found, so there is nothing to be excepted. – matszwecja Mar 14 '22 at 15:57
  • if it doesn't find the "solved" then it doesn't throw exception (sorry if it's a noob question, I don't have that much coding background)? – Karuvägistaja Mar 14 '22 at 15:58
  • 1
    Yup, based on Selenium docs: [If there’s no attribute with that name, None is returned.](https://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.get_attribute) – matszwecja Mar 14 '22 at 15:59
  • So the best solution would be to add "else" statement that returns "false"? As it's automatic captcha solving script then should I also increase the sleep timer from 10 seconds as it would only then check once and if it doesn't find it it gives up, right? – Karuvägistaja Mar 14 '22 at 16:04
  • [Minor nitpick](https://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice): ```except``` without an exception are considered bad practice. – Tempman383838 Mar 14 '22 at 16:40

0 Answers0