2

Even though my password is correct. I can't log in to Instagram and it gives your password is incorrect error. My intention is to enter Instagram and follow some users automatically. However, I am totally stuck at the login screen. Can the language and connection country be a problem?

from selenium import webdriver    
from selenium.webdriver.common.by import By as by    
from time import sleep    
from selenium.webdriver.common.keys import Keys    

     
    URL = "https://www.instagram.com/natgeo/"
    URL_LOGIN = "https://www.instagram.com/accounts/login/"
    USERNAME = "id"
    PASSWORD = 'pass'
     

 

    class InstaFollower:
        def __init__(self):
            self.driver = webdriver.Chrome("C:\Selenium\chromedriver.exe")

 
    def login(self):
        self.driver.get(URL_LOGIN)
        sleep(3)
        self.driver.find_element(by.XPATH, '/html/body/div[4]/div/div/button[1]').click()
        sleep(5)
        for x in USERNAME:
            self.driver.find_element(by.CSS_SELECTOR,
                                     'input[aria-label="Telefon numarası, kullanıcı adı veya e-posta"]').send_keys(x)
            sleep(.3)
        sleep(5)
        self.driver.find_element(by.CSS_SELECTOR, 'input[aria-label="Şifre"]').clear()
        passy = self.driver.find_element(by.CSS_SELECTOR, 'input[aria-label="Şifre"]')
        for i in PASSWORD:
            passy.send_keys(i)
            sleep(.3)
        sleep(5)
        passy.send_keys(Keys.ENTER)
 
        sleep(5)
        self.driver.find_element(by.CSS_SELECTOR, 'button[class="sqdOP  L3NKy   y3zKF     "]').click()
        sleep(5)
        self.driver.find_element(by.CSS_SELECTOR, 'button[class="aOOlW   HoLwm "]').click()
        sleep(5)
        self.driver.get(URL)
 
    def find_followers(self):
        self.driver.find_element(by.CSS_SELECTOR, 'a[class="-nal3 "]').click()
        followers = self.driver.find_elements(by.CSS_SELECTOR, 'button[class="sqdOP  L3NKy   y3zKF     "]')
        print(followers)
        return followers
 
    def follow(self, followers):
        for follower in followers:
            follower.click()
            print("Followed!")
        self.driver.find_element(by.CSS_SELECTOR, 'button[class="wpO6b  "]').click()
 
 
bot = InstaFollower()
bot.login()
followers = bot.find_followers()
bot.follow(followers)

My Solution: Password correction was done and after that, I changed the user following algorithm and in the end, it works perfectly but still, there is an improvement chance in which I can follow more people at once. I am going to try to do it in a simple way again but currently have no idea about it. :) from selenium import webdriver
from selenium.webdriver.common.by import By as by
from time import sleep
from selenium.webdriver.common.keys import Keys

    URL = "https://www.instagram.com/natgeo/"
    URL_LOGIN = "https://www.instagram.com/accounts/login/"
    USERNAME = "id"
    PASSWORD = 'pass'


class InstaFollower:
    def __init__(self):
        self.driver = webdriver.Chrome(executable_path="C:\Selenium\chromedriver.exe")

    def login(self):
        self.driver.get("https://www.instagram.com/accounts/login/")
        sleep(5)
        self.driver.find_element(by.XPATH, '/html/body/div[4]/div/div/button[1]').click()
        usr = self.driver.find_element(by.CSS_SELECTOR,
                                       'input[aria-label="Telefon numarası, kullanıcı adı veya e-posta"]')
        for x in USERNAME:
            usr.send_keys(x)
            sleep(.1)
        sleep(2)
        passy = self.driver.find_element(by.CSS_SELECTOR, 'input[aria-label="Şifre"]')
        for i in PASSWORD:
            passy.send_keys(i)
            sleep(.1)
        passy.send_keys(Keys.ENTER)
        sleep(5)
        self.driver.find_element(by.CSS_SELECTOR, 'div.cmbtv').click()
        sleep(5)
        self.driver.find_element(by.CSS_SELECTOR, 'button.HoLwm').click()

    def find_followers(self):
        self.driver.get(URL)
        self.driver.find_element(by.CSS_SELECTOR, 'a.-nal3').click()

    def follow(self):
        x=0
        sleep(5)
        follower_screen=self.driver.find_elements(by.CSS_SELECTOR,'button.sqdOP.L3NKy.y3zKF')
        for f in follower_screen:
            f.click()
            try:
                self.driver.find_element(by.CSS_SELECTOR,'button.aOOlW.HoLwm').click()
            except:
                pass
            print("Followed!")
            sleep(.7)

bot = InstaFollower()
bot.login()
followers = bot.find_followers()
bot.follow()

1 Answers1

1

The login fields on Instagram Login Page are ReactJS elements. So to send a character sequence to those fields you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.instagram.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("MuhammetF.Eren")
    driver.find_element(By.CSS_SELECTOR, "input[name='password']").send_keys("MuhammetF.Eren")
    
  • Using XPATH:

    driver.get('https://www.instagram.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("MuhammetF.Eren")
    driver.find_element(By.XPATH, "//input[@name='password']").send_keys("MuhammetF.Eren")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

instagram_username_password


References

You can find a couple of relevant detailed discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I actually tried this and this is not working. :( – Elentronics Jan 24 '22 at 19:34
  • What error do you see execution the xpath or css based solution from this answer? – undetected Selenium Jan 24 '22 at 19:35
  • 1
    The thing is I dont get any execution error. When my code enters my password, The Instagram gives wrong password error. – Elentronics Jan 24 '22 at 19:41
  • 1
    @MuhammetF.Eren Observe the screenshot within my answer, the password I'm passing through `send_keys()` just gets printed within the password field. – undetected Selenium Jan 24 '22 at 19:43
  • 1
    @MuhammetF.Eren, the password is a fake, just for a test. Replace it with your own username and password. – The Amateur Coder Jan 24 '22 at 20:02
  • 1
    Yep. I know it is fake but it doesn't work with my original password and id. :/ – Elentronics Jan 24 '22 at 20:13
  • @MuhammetF.Eren, oh, in that case, try making a new account. The code works fine for me...I tried making a new account, but Instagram shows this: "Sorry, something went wrong creating your account. Please try again soon.". I think it's a problem with Instagram and not the code. I tried using a ProtonMail account, and I was on a private Brave window. Is that the problem? – The Amateur Coder Jan 24 '22 at 21:59
  • There is a problem with code for sure but I don't know what it is maybe I close cookies. I will try your method but not so hopefull. I hope new account will work even though that account was already new. – Elentronics Jan 25 '22 at 19:43