Python Forum
Error: variable can not be defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error: variable can not be defined
#1
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?
Reply
#2
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.
Reply
#3
omg, I'm stupid. Thank you ! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 3 111 Yesterday, 02:13 PM
Last Post: Larz60+
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 516 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,166 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,044 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Error 'Contour' not Defined DaveG 3 2,283 Mar-13-2022, 03:29 AM
Last Post: deanhystad
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 3,528 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 2,334 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 2,123 Dec-14-2020, 05:45 PM
Last Post: karabakh
  Function will not return variable that I think is defined Oldman45 6 3,435 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,164 Aug-12-2020, 10:34 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020