使用 Python Selenium 自动登录 Instagram
今天,我们将使用 Selenium 登录 Instagram 来完成这项任务。 需要包装: 硒包 # for Windows pip install selenium # for Linux/Max pip3 install selenium # or sudo -H pip3 install selenium 进入全屏模式 退出全屏模式 Chromedriver 兼容现有chrome版本下载 进
·
今天,我们将使用 Selenium 登录 Instagram 来完成这项任务。
需要包装:
- 硒包
# for Windows
pip install selenium
# for Linux/Max
pip3 install selenium
# or
sudo -H pip3 install selenium
进入全屏模式 退出全屏模式
- Chromedriver 兼容现有chrome版本下载
进口:
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()
进入全屏模式 退出全屏模式
更多推荐
已为社区贡献126473条内容
所有评论(0)