Python Forum

Full Version: Error: variable can not be defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
THE OTHER PART OF MY CODE ABOVE IS NOT NECASSERY

size_1 = driver.find_element_by_css_selector('#productSizeStock .btn-default:nth-child(1)').\
    get_attribute('title').replace('Selecteer maat ', '')
try:
    size_2 = driver.find_element_by_css_selector('#productSizeStock .btn-default:nth-child(2)').\
        get_attribute('title').replace('Selecteer maat ', '')
except Exception:
    size_title = 'Size'
    sizes = size_1
else:
    try:
        size_title = 'Sizes'
        size_3 = driver.find_element_by_css_selector('#productSizeStock .btn-default:nth-child(3)').\
            get_attribute('title').replace('Selecteer maat ', '')
    except Exception:
        sizes = size_1 + ' & ' + size_2
    else:
        try:
            size_4 = driver.find_element_by_css_selector('#productSizeStock .btn-default:nth-child(4)').\
                get_attribute('title').replace('Selecteer maat ', '')
        except Exception:
            sizes = size_1 + ', ' + size_2 + ' & ' + size_3
        else:
            try:
                size_5 = driver.find_element_by_css_selector('#productSizeStock .btn-default:nth-child(5)').\
                    get_attribute('title').replace('Selecteer maat ', '')
            except Exception:
                sizes = size_1 + ', ' + size_2 + ', ' + size_3 + ' & ' + size_4
            else:
                try:
                    size_6 = driver.find_element_by_css_selector(
                        '#productSizeStock .btn-default:nth-child(6)').get_attribute('title').\
                        replace('Selecteer maat ','')
                except Exception:
                    sizes = size_1 + ', ' + size_2 + ', ' + size_3 + ', ' + size_4 + ' & ' + size_5
                else:
                    try:
                        size_7 = driver.find_element_by_css_selector('#productSizeStock .btn-default:nth-child(7)').\
                            get_attribute('title').replace('Selecteer maat ', '')
                    except Exception:
                        sizes = size_1 + ', ' + size_2 + ', ' + size_3 + ', ' + size_4 + ', ' + size_5 + ' & ' + size_6
                    else:
                        try:
                            size_8 = driver.find_element_by_css_selector\
                                ('#productSizeStock .btn-default:nth-child(8)').\
                                get_attribute('title').replace('Selecteer maat ', '')
                        except Exception:
                            sizes = size_1 + ', ' + size_2 + ', ' + size_3 + ', ' + size_4 + ', ' + size_5 + ', ' \
                                    + size_6 + ' & ' + size_7
                        else:
                            try:
                                size_9 = driver.find_element_by_css_selector\
                                    ('#productSizeStock .btn-default:nth-child(9)').\
                                    get_attribute('title').replace('Selecteer maat ', '')
                            except Exception:
                                sizes = size_1 + ', ' + size_2 + ', ' + size_3 + ', ' + size_4 + ', ' + size_5 \
                                        + ', ' + size_6 + ', ' + size_7 + ' & ' + size_8
print(sizes)
Error:
Traceback (most recent call last): File "C:/Users/Julius/PycharmProjects/Fenix/venv/Klad2.py", line 72, in <module> print(sizes) NameError: name 'sizes' is not defined
So my code does the following:
With '.get_attribute' it converts the css path from the size into the actual size the website displays. I use 'Try:' because it could be possible that there are only 3 sizes instock, but it could also be 8. So it constantly defines sizes after it converted the css path to the size.

I do not understand why it gives the error: name 'sizes' is not defined. Could somebody help?
I can't see any binding of the name "sizes" that's not in an except clause. So, if your code executes all the try blocks successfully, then you'll be trying to print a variable that doesn't exist.
omg, I'm stupid. Thank you ! :)