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
TraceBackwould help to undertand the problem...Pls, add complete error along with questionfollowersis empty or has ONLY one element (if it is a list, set or dict). Debug the functionfind_elements_by_class_name()to find out the reason.