'Python selenium: UnboundLocalError: local variable 'followin' referenced before assignment

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


Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium