0

How to make this nested loop with exceptions print out success, if it catches the NoSuchElementException?

for key, value in a.items():
  for key, value in b.items():
    if “abc” not in value:
        driver.get(url)
        try:
            if pyautogui.locateCenterOnScreen(death, grayscale=True, confidence=.3) is not None:
              driver.close()
            elif pyautogui.locateCenterOnScreen(death2, grayscale=True, confidence=.3) is not None:
              driver.close()
            elif driver.find_element_by_xpath(element1).is_displayed():
              driver.close()
            else:
                if pyautogui.locateCenterOnScreen(element2, grayscale=True, confidence=.5) is not None:
                    break
                elif driver.find_element_by_xpath(element3).is_displayed():
                    break
                else:
                    driver.close()
        except NoSuchElementException as nse:
           **GOTO print(‘SUCCESS!’)**
        except Exception as eeee:
            driver.close()
    else:
        print('this should never appear!')

  print(‘SUCCESS!’)
t_k
  • 121
  • 2
  • 8
  • 2
    This might answer your question, https://stackoverflow.com/questions/2597104/break-the-nested-double-loop-in-python – DrummerMann Jan 11 '22 at 01:01
  • 2
    Just use `break`. – PM 77-1 Jan 11 '22 at 01:06
  • 1
    That goto statement would break out of **both** loops. But the post title says just break out of the second loop. Which is it? – John Gordon Jan 11 '22 at 01:07
  • Workaround with: raise Found; except Found? I like it. Gonna take a while to test this program out, so appreciate your input if this is correct, and whether I can also use it instead of the other cases with break. Thanks for this, if it works, it's a clean, intuitive solution. – t_k Jan 11 '22 at 01:08
  • John, I have used two-space indent there because I had problems with formatting. – t_k Jan 11 '22 at 01:11
  • 1
    Ahh, I see. If you only want to break out of the inner loop, then just use `break`, as @PM77-1 said. What is the difficulty? – John Gordon Jan 11 '22 at 01:15
  • You many not need two loops: `for (key1, value1), (key2, value2) in itertools.product(a.items(), b.items()):` – chepner Jan 11 '22 at 01:29
  • You really shouldn't use exceptions for success... that's just wrong on so many levels! – JeffUK Jan 11 '22 at 01:30

1 Answers1

0
for key, value in a.items():
  _break = False
  for key, value in b.items():
    if “abc” not in value:
        driver.get(url)
        try:
            if pyautogui.locateCenterOnScreen(death, grayscale=True, confidence=.3) is not None:
              driver.close()
            elif pyautogui.locateCenterOnScreen(death2, grayscale=True, confidence=.3) is not None:
              driver.close()
            elif driver.find_element_by_xpath(element1).is_displayed():
              driver.close()
            else:
                if pyautogui.locateCenterOnScreen(element2, grayscale=True, confidence=.5) is not None:
                    break
                elif driver.find_element_by_xpath(element3).is_displayed():
                    break
                else:
                    driver.close()
        except NoSuchElementException as nse:
           _break = True
           break
        except Exception as eeee:
            driver.close()
    else:
        print('this should never appear!')
  if _break:
    break

print(‘SUCCESS!’)

you can use a variable to control the outer loop.

baozilaji
  • 304
  • 1
  • 10