简体   繁体   中英

How to raise NoSuchElementException inside another NoSuchElementException

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?

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:
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM