简体   繁体   中英

Selenium Instagram Bot Follower

I want to open an Instagram account, tap on their follower and follow all their followers. So I've written a selenium program. This code opens profile, then login with my username and password then tap on the followers, But after that, it doesn't follow all the followers. Please help.

from selenium import webdriver
class InstaBot:
   

    def __init__(self):
           self.driver = webdriver.Firefox()
           self.driver.implicitly_wait(10)
           self.driver.get("https://www.instagram.com/randomusername/")
       
   def get_follower(self, username, pw):
       self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/ul/li[3]/a")\
           .click()
      
      
       self.driver.find_element_by_xpath("//input[@name=\"username\"]")\
           .send_keys(username)
      
       self.driver.find_element_by_xpath("//input[@name=\"password\"]")\
           .send_keys(pw)
 
       self.driver.find_element_by_xpath('//button[@type="submit"]')\
           .click()
 
       self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/section/div/button")\
           .click()
 
       self.driver.find_element_by_xpath("/html/body/div[1]/section/main/div/header/section/ul/li[2]/a")\
           .click()

       
       follow = self.driver.find_elements_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[3]/div/div[2]")

       i =1

       for follower in follow:
            if(i !=1):
                self.driver.find_elements_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[1]/div/div[3]")\
                    .click()

                i += 1   
      


       
 
 
my_bot = InstaBot()
my_bot.get_follower('username', 'pw')

The last two xpaths suggest you're only finding one element.

Try print(len(follow)) and it should match the number of elements you're expecting.

And then when you iterate through the follow list, that xpath is selecting one element in the DOM, the same element each time. You're not doing anything with the previous step so I don't think that's even necessary--you just need a better xpath. One that will select all of the elements you want to click

If this doesn't get you what you need (and you decide not to ditch it for the API) then I can help more tonight

Also this is a great case for enumerate. It handles the incrementing logic for you

       for i, follower in enumerate(follow):
            if i == 1: continue
            self.driver.find_elements_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[1]/div/div[3]")\
                    .click()

The last two xpaths suggest you're only finding one element.

Try print(len(follow)) and it should match the number of elements you're expecting.

And then when you iterate through the follow list, that xpath is selecting one element in the DOM, the same element each time. You're not doing anything with the previous step so I don't think that's even necessary--you just need a better xpath. One that will select all of the elements you want to click

If this doesn't get you what you need (and you decide not to ditch it for the API) then I can help more tonight

Also this is a great case for enumerate. It handles the incrementing logic for you

 here for i, follower in enumerate(follow):
        if i == 1: continue
        self.driver.find_elements_by_xpath("/html/body/div[5]/div/div/div[2]/ul/div/li[1]/div/div[3]")\
                .click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM