0

I am trying to get my followers and the followers of my followers to make a graph for a project Below is the function that i get the error from. The error from the followers[1].click(). I tried several things to fix this but nothing works. To give you a better perspective. The python script automatically opens the google chrome, fills my credentials and navigates to my profile. Then it stuck there and throws the error.


    def get_my_followers(self, username):
        self.go_to_page("https://instagram.com/" + username + "/")
        time.sleep(5)
        my_followers_set = set()
        followers = self.driver.find_elements_by_class_name("-nal3")
        followers[1].click()
        time.sleep(2)
        initialise_vars = 'elem = document.getElementsByClassName("isgrP")[0]; followers = parseInt(document.getElementsByClassName("g47SY")[1].innerText); times = parseInt(followers * 0.14); followersInView1 = document.getElementsByClassName("FPmhX").length'
        initial_scroll = 'elem.scrollTop += 500'
        next_scroll = 'elem.scrollTop += 1500'

        with open('./jquery-3.3.1.min.js', 'r') as jquery_js:
            # 3) Read the jquery from a file
            jquery = jquery_js.read()
            # 4) Load jquery lib
            self.driver.execute_script(jquery)
            # scroll down the page
            self.driver.execute_script(initialise_vars)
            #self.driver.execute_script(scroll_followers)
            self.driver.execute_script(initial_scroll)
            time.sleep(3)

            next = True
            while(next):
                n_li_1 = len(self.driver.find_elements_by_class_name("FPmhX"))
                self.driver.execute_script(next_scroll)
                time.sleep(1.5)
                n_li_2 = len(self.driver.find_elements_by_class_name("FPmhX"))
                if(n_li_1 != n_li_2):
                    following = self.driver.find_elements_by_xpath("//*[contains(text(), 'Following')]")
                    for follow in following:
                        el = follow.find_element_by_xpath('../..')
                        el = el.find_element_by_tag_name('a')
                        profile = el.get_attribute('href')
                        my_followers_set.add(profile)
                else:
                    next = False

            return list(my_followers_set)

This is the trace back of the error

Traceback (most recent call last):
  File "get_my_followers.py", line 34, in <module>
    get_my_followers(config)
  File "get_my_followers.py", line 20, in get_my_followers
    my_followers = b.get_my_followers(username)
  File "C:\Users\user\PycharmProjects\testing\bot.py", line 74, in get_my_followers
    followers[1].click()
IndexError: list index out of range
5
  • Complete TraceBack would help to undertand the problem...Pls, add complete error along with question Commented Oct 14, 2022 at 13:52
  • @Bhargav just updated with the traceback Commented Oct 14, 2022 at 13:58
  • 1
    Clearly the object followers is empty or has ONLY one element (if it is a list, set or dict). Debug the function find_elements_by_class_name() to find out the reason. Commented Oct 14, 2022 at 14:00
  • followers = self.driver.find_elements_by_class_name("-nal3") is the problem. Best to check for size/count of the array before addressing an index as in followers[1].click() Commented Oct 14, 2022 at 18:28
  • @pcalkins i couldnt locate the problem Commented Oct 15, 2022 at 14:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.