Home > Mobile >  Python selenium: UnboundLocalError: local variable 'followin' referenced before assignment
Python selenium: UnboundLocalError: local variable 'followin' referenced before assignment

Time:02-19

Code block:

class accinfo():
    def getfolowing(acclink):
        time.sleep(2)
        profileurl = "https://instagram.com/"   (acclink)   "/"
        browser.get(profileurl)
        time.sleep(2)
        wait.until(EC.visibility_of_element_located(
            (By.XPATH, '//*[@id="react-root"]/section/main/div/header/section/ul/li[3]/a/div'))).click()

        ##accinfo.scroldownflowng()
        following = []
        following = browser.find_elements(By.CLASS_NAME, 'notranslate._0imsa')
        for followin in following:
            print(followin.text)
        print(len(followin))


time.sleep(10)
accinfo.goprofile(hedefhesap, loginfo1)
accinfo.getfollower(hedefhesap)
accinfo.getfolowing(hedefhesap)

I'm gettin error, how can I fix it?

Traceback (most recent call last):
  File "C:/Users/ayarb/PycharmProjects/instagrambotv1/main.py", line 112, in <module>
    accinfo.getfolowing(hedefhesap)
  File "C:/Users/ayarb/PycharmProjects/instagrambotv1/main.py", line 62, in getfolowing
    print(len(followin))
UnboundLocalError: local variable 'followin' referenced before assignment

CodePudding user response:

You try to use local variable followin, that you create here:

for followin in following:

You better initialize variable before use it. Just add:

followin = null

at the beginning

P.S.

When you use loop for you create a local variable that exist only in loop. When you exit the loop, your variable followin just disappear.

CodePudding user response:

This error message...

UnboundLocalError: local variable 'followin' referenced before assignment

...implies that you have referenced the variable followin even before it was assigned any value.


The list following is created with 0 elements i.e.

print(len(followin))

would print 0.

But even before that within the for() loop you have tried to iterate the list elements through the variable followin which never gets initialized. Hence you see the error.

  • Related