Python Selenium을 사용하여 Instagram 로그인 자동화
4730 단어 pythonautomationselenium
필요한 패키지:
# for Windows
pip install selenium
# for Linux/Max
pip3 install selenium
# or
sudo -H pip3 install selenium
수입:
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)
여기에는 두 가지 기능이 있습니다.
테스트 코드:
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()
Reference
이 문제에 관하여(Python Selenium을 사용하여 Instagram 로그인 자동화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/itsvinayak/automate-instagram-login-using-python-selenium-m2p텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)