Python Selenium을 사용하여 Instagram 로그인 자동화

오늘은 이 작업을 수행하기 위해 Selenium을 사용하여 Instagram에 로그인합니다.

필요한 패키지:
  • 셀레늄 패키지

  • # for Windows
    pip install selenium
    
    # for Linux/Max
    pip3 install selenium
    # or
    sudo -H pip3 install selenium
    
    


  • 기존 크롬 버전과 호환되는 크롬드라이버download

  • 수입:

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    import time
    
    


    상수:

    URL = "https://www.instagram.com/accounts/login/"
    EXE_PATH = r"C:\Users\vinayak\Documents\chromedriver_win32\chromedriver"
    
    


    ChromeDriver 클래스:

    
    class ChromeDriver:
        def __init__ (self, username, password):
            self.username = username
            self.password = password
            self.driver = webdriver.Chrome(executable_path=EXE_PATH)
            self.driver.implicitly_wait(10)
    
        def login(self):
            self.driver.get(URL)
            self.driver.maximize_window()
            self.driver.find_element_by_xpath(
                "/html//form[@id='loginForm']/div//input[@name='username']"
            ).send_keys(self.username)
            self.driver.find_element_by_xpath(
                "/html//form[@id='loginForm']/div/div[2]/div//input[@name='password']"
            ).send_keys(self.password)
            self.driver.find_element_by_xpath(
                "/html//form[@id='loginForm']//button[@type='submit']"
            ).click()
            # to clean popups after login
            self.afterLogin()
    
        def afterLogin(self):
            try:
                self.driver.find_element_by_xpath(
                    "//button[contains(text(),'Save Info')]"
                ).click()
            except NoSuchElementException:
                print("no save Info")
            try:
                self.driver.find_element_by_xpath(
                    "//*[contains(@class, 'aOOlW HoLwm ')]"
                ).click()
            except NoSuchElementException:
                print("no notification box")
            time.sleep(100)
    
    


    여기에는 두 가지 기능이 있습니다.
  • 로그인: 이 기능은 Instagram을 열고 Instagram에 로그인합니다
  • .
  • afterlogin: Instagram에 로그인한 후 일부 팝업이 표시되면 이 기능이 팝업을 제거합니다
  • .

    테스트 코드:

    def main():
        username = input("enter username: ")
        password = input("enter password: ")
        test = ChromeDriver(username, password)
        test.login()
    
    if __name__ == " __main__":
        main()
    
    


    전체 코드:

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    import time
    
    # constants
    URL = "https://www.instagram.com/accounts/login/"
    EXE_PATH = r"C:\Users\vinayak\Documents\chromedriver_win32\chromedriver"
    
    class ChromeDriver:
        def __init__ (self, username, password):
            self.username = username
            self.password = password
            self.driver = webdriver.Chrome(executable_path=EXE_PATH)
            self.driver.implicitly_wait(10)
    
        def login(self):
            self.driver.get(URL)
            self.driver.maximize_window()
            self.driver.find_element_by_xpath(
                "/html//form[@id='loginForm']/div//input[@name='username']"
            ).send_keys(self.username)
            self.driver.find_element_by_xpath(
                "/html//form[@id='loginForm']/div/div[2]/div//input[@name='password']"
            ).send_keys(self.password)
            self.driver.find_element_by_xpath(
                "/html//form[@id='loginForm']//button[@type='submit']"
            ).click()
            # to clean popups after login
            self.afterLogin()
    
        def afterLogin(self):
            try:
                self.driver.find_element_by_xpath(
                    "//button[contains(text(),'Save Info')]"
                ).click()
            except NoSuchElementException:
                print("no save Info")
            try:
                self.driver.find_element_by_xpath(
                    "//*[contains(@class, 'aOOlW HoLwm ')]"
                ).click()
            except NoSuchElementException:
                print("no notification box")
            time.sleep(100)
    
    def main():
        username = input("enter username: ")
        password = input("enter password: ")
        test = ChromeDriver(username, password)
        test.login()
    
    if __name__ == " __main__":
        main()
    
    

    좋은 웹페이지 즐겨찾기