3

this is my first time posting so if I made a mistake please let me know.

I'm trying to make a bot for fun on Instagram and one of the functions is to go to a user's page, view their followers, and then start following people on that list.

This post was similar however the list I'm pulling from repopulates more entries after scrolling through every 12 profiles.

Here's the code so far:

def follow_multiple(self, user):
        #navigate to user profile
        self.nav_user(user)
        time.sleep(3)

        #open followers window
        followers_button = self.driver.find_element_by_xpath("//a[contains(@href, '/{}/followers')]".format(user))
        followers_button.click()
        time.sleep(2)
        followers_popup = self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]")

        follower_popup_range = int(self.follower_amt(user))
        for i in range(int(follower_popup_range/11)):
            time.sleep(random.normalvariate(2, 0.3))
            f = 1
            while f<=12:
                
                try:
                    #try to click follow button
                    time.sleep(random.normalvariate(1.2, 0.2))   
                    lc = f + (i*11)
                    print('pressing' + str(lc) + '...')
                    self.driver.find_element_by_xpath("/html/body/div[4]/div/div/div[2]/ul/div/li[{}]/div/div[3]/button"
                        .format(lc)).click()
                    print('button ' + str(lc) + ' pressed')

                except:
                    print('button ' + str(lc) + 'failed')
                    time.sleep(random.normalvariate(0.2, 0.04))
                    #click cancel

                    try:
                        #attempt to click cancel on one xpath
                        self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div/div[3]/button[2]").click()
                        print(str(lc) + ': Already following user')
                        f -= 1

                    except:
                        #other xpath for the same button
                        time.sleep(1)
                        self.driver.find_element_by_xpath("/html/body/div[5]/div/div/div/div[3]/button[2]").click()

                f += 1 
            #JS to scroll through list
            self.driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', followers_popup)
            time.sleep(2)

It has no problem going to the page and opening the follower box. And the outer loop can scroll through the box just fine. The inner loop dies at the 12th element just after the print saying 'Button 12 failed'

The inner loop should go through and follow each account (12 elements each population) and the try block is there to handle clicking on someone already being followed that brings up a confirmation page that it hits cancel. Just not sure why it dies at the end of the 12th and final iteration it gives me this error:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document 

But I don't know why since the element is there. Any help is greatly appreciated. Thanks!

  • 1
    check out this answer https://stackoverflow.com/a/12967602/5266746 - it's for Java, but the same basic principles. – n1c9 Oct 02 '20 at 04:49
  • 1
    Does this answer your question? [StaleElementReferenceException on Python Selenium](https://stackoverflow.com/questions/27003423/staleelementreferenceexception-on-python-selenium) – englealuze Oct 02 '20 at 04:58
  • 1
    instead of using Xpath, consider using find_element_by_id or by_class because the html of the page of web apps like instagram can change based on user interaction. This might cause errors in the execution – Abdul Rauf Oct 02 '20 at 09:16
  • @AbdulRauf I tried xpath for a button containing text but it kept saying the button was not clickable at point (x,y) and said another object was being clicked instead. – Brandon Pardi Oct 04 '20 at 00:56

0 Answers0