# Buggy code
def get_given_user_followers(
    browser, user_name, amount, dont_include, login, follow_restrict, is_random
):
    browser.get("https://www.instagram.com/" + user_name)

    #  check how many poeple are following this user.
    allfollowing = formatNumber(browser.find_element_by_xpath("//li[2]/a/span").text)

    #  throw RuntimeWarning if we are 0 people following this user
    if allfollowing == 0:
        raise RuntimeWarning("There are 0 people to follow")

    try:
        following_link = browser.find_elements_by_xpath(
            '//a[@href="/' + user_name + '/followers/"]'
        )
        following_link[0].send_keys("\n")
    except BaseException as e:
        print("following_link error \n", str(e))

    followNum = 0
    sleep(2)
    person_followed = []

    # find dialog box
    dialog = browser.find_element_by_xpath(
        "//div[text()='Followers']/following-sibling::div"
    )

    # scroll down the page
    scroll_bottom(browser, dialog, allfollowing)

    # Get follow buttons. This approch will find the follow buttons and ignore the Unfollow/Requested buttons.
    follow_buttons = dialog.find_elements_by_xpath(
        "//div/div/span/button[text()='Follow']"
    )
    person_list = []

    if amount >= len(follow_buttons):
        amount = len(follow_buttons)
        print(user_name + " -> Less users to follow than requested.")

    finalBtnPerson = []
    if is_random:
        sample = random.sample(range(0, len(follow_buttons)), amount)

        for num in sample:
            finalBtnPerson.append(follow_buttons[num])
    else:
        finalBtnPerson = follow_buttons[0:amount]
    for person in finalBtnPerson:
        if person and hasattr(person, "text") and person.text:
            person_list.append(
                person.find_element_by_xpath("../../../*")
                .find_elements_by_tag_name("a")[1]
                .text
            )

    return person_list


# Traceback (selenium.common.exceptions.NoSuchElementException)
Logged in successfully!
Traceback (most recent call last):
File "test4_instapy.py", line 24, in <module>
session.interact_user_followers(['User1'], amount=12, random=True)
File "/virt_uni/InstaPy/instapy/instapy.py", line 849, in interact_user_followers
userToInteract += get_given_user_followers(self.browser, user, amount, self.dont_include, self.username, self.follow_restrict, random)
File "/virt_uni/InstaPy/instapy/unfollow_util.py", line 327, in get_given_user_followers
allfollowing = formatNumber(browser.find_element_by_xpath("//li[2]/a/span").text)
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 293, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 752, in find_element
'value': value})['value']
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.4/dist-packages/selenium/webdriver/remote/errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//li[2]/a/span"}
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.29.461571 (8a88bbe0775e2a23afda0ceaf2ef7ee74e822cc5),platform=Linux 3.16.0-4-amd64 x86_64)

# Fixed code
def get_given_user_followers(
    browser, user_name, amount, dont_include, login, follow_restrict, is_random
):
    browser.get("https://www.instagram.com/" + user_name)

    # check how many poeple are following this user.
    # throw RuntimeWarning if we are 0 people following this user or
    # if its a private account
    try:
        allfollowing = formatNumber(
            browser.find_element_by_xpath("//li[2]/a/span").text
        )
    except NoSuchElementException:
        print("Can't interact with private account")
        return

    following_link = browser.find_elements_by_xpath(
        '//a[@href="/' + user_name + '/followers/"]'
    )
    following_link[0].send_keys("\n")

    sleep(2)

    # find dialog box
    dialog = browser.find_element_by_xpath(
        "//div[text()='Followers']/following-sibling::div"
    )

    # scroll down the page
    scroll_bottom(browser, dialog, allfollowing)

    # get follow buttons. This approch will find the follow buttons and
    # ignore the Unfollow/Requested buttons.
    follow_buttons = dialog.find_elements_by_xpath(
        "//div/div/span/button[text()='Follow']"
    )
    person_list = []

    if amount >= len(follow_buttons):
        amount = len(follow_buttons)
        print(user_name + " -> Less users to follow than requested.")

    finalBtnPerson = []
    if is_random:
        sample = random.sample(range(0, len(follow_buttons)), amount)

        for num in sample:
            finalBtnPerson.append(follow_buttons[num])
    else:
        finalBtnPerson = follow_buttons[0:amount]
    for person in finalBtnPerson:
        if person and hasattr(person, "text") and person.text:
            person_list.append(
                person.find_element_by_xpath("../../../*")
                .find_elements_by_tag_name("a")[1]
                .text
            )

    return person_list