How to raise NoSuchElementException inside another NoSuchElementException

Question:

I have a whatsapp bot and I need to check for multiple elements in selenium one after the other like:

try:
  ....
except NoSuchElementException:
  ...
  try:
    ...
  except NoSuchElementException

I know that this isn’t possible as it would raise During handling the above exception, another exception occured… and that stuff, I tried to use if as work around, but didn’t work. Does anyone have any way to achieve this?

Asked By: Omar Yacop

||

Answers:

This back to back try-catch{} block:

try:
  ....
except NoSuchElementException:
  ...
  try:
    ...
  except NoSuchElementException
  

have an indentation issue.


Solution

You can check for multiple elements through multiple try-catch{} bolck as follows:

try:
  ....
  try:
    ...
  except NoSuchElementException:
    ...
except NoSuchElementException:
  ...
Answered By: undetected Selenium