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
  Getting "name 'get_weather' is not defined error and no json_data returned? trthskr4 6 452 Sep-14-2021, 09:55 AM
Last Post: trthskr4
  Error when refering to class defined in 'main' in an imported module HeRo 2 670 Apr-13-2021, 07:22 PM
Last Post: HeRo
  Why does lambda throw 'name value_o is not defined' error? karabakh 3 719 Dec-14-2020, 05:45 PM
Last Post: karabakh
  Function will not return variable that I think is defined Oldman45 6 1,357 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 1,219 Aug-12-2020, 10:34 PM
Last Post: snippsat
  name error "name"is not defined MaartenRo 1 1,114 Jul-28-2020, 02:39 AM
Last Post: bowlofred
  Variable not defined Heyjoe 4 983 Jul-10-2020, 11:27 PM
Last Post: Heyjoe
  Name Error: name 'Stockton' is not defined Pinokchu 3 957 Jun-13-2020, 02:48 PM
Last Post: Yoriz
  python library not defined in user defined function johnEmScott 2 983 May-30-2020, 04:14 AM
Last Post: DT2000
  error ,,name append is not defined'' Killdoz 1 1,893 May-24-2020, 06:23 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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