2

Error:

unindent does not match any outer indentation level

Code:

def confirmation(self):
    if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
        print("Checkout Failed!")

     if WebDriverWait(self.driver, 1000).until(
EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
        print("Checkout Succesful!")

How can I address the error?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
ufo361
  • 35
  • 1
  • 5
  • Does this answer your question? [IndentationError: unindent does not match any outer indentation level](https://stackoverflow.com/questions/492387/indentationerror-unindent-does-not-match-any-outer-indentation-level) – Tomerikoo Jan 24 '22 at 08:45

3 Answers3

3

This error message...

unindent does not match any outer indentation level

...implies that there is a problem with the indentation in your code block.

This error is mostly observed when there is a mixup of Tab and Space characters while indenting Python code.


Analysis

When I copied you code block and executed:

indentation_problem

I got the same error:

C:\Users\username\Desktop\directory\PyPrograms>class_in_python.py
  File "C:\Users\Soma Bhattacharjee\Desktop\Debanjan\PyPrograms\class_in_python.py", line 26
    def confirmation(self):
              ^
IndentationError: unindent does not match any outer indentation level

Once, I removed all the white paces and the new line character and break up the line again for the following line of code, the default indentation seems to be different:

     if WebDriverWait(self.driver, 1000).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
        print("Checkout Succesful!")

indentation_difference


Solution

You need to fix up the indentation of the following lines:

  • EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
  • EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):

You can find a relevant detailed discussion in Is there something issue in indentation, while using Anaconda's Spyder

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

The indent of the 2nd 'if' does not match the first, it is in one extra space. In python, your indentation must be consistent

Basically the places you could indent that 2nd 'if' would be even with the first 'if' or even with the first 'print' (in which case the 2nd 'if' would be within the first 'if'). The current location of the 2nd 'if' doesn't match any of these previous levels so python throws an error.

user2199860
  • 788
  • 4
  • 14
0

You have a new line after .until( python is considering this a new line of code thus throwing the error. Also your second if and print have an extra space before them.

def confirmation(self):
    if WebDriverWait(self.driver, 1000).until(
        EC.visibility_of_element_located((By.CLASS_NAME, 'failed'))):
            print("Checkout Failed!")

    if WebDriverWait(self.driver, 1000).until(
      EC.visibility_of_element_located((By.CLASS_NAME, 'succesful'))):
          print("Checkout Succesful!")
Jab
  • 26,853
  • 21
  • 75
  • 114