Python Instagram 自动记录器
Python Instagram Auto logger
我只是想制作一个 python API 自动将我记录在我的 Instagram 页面上,但该程序似乎无法识别必须放置我的电子邮件地址的框和我的密码。我使用了通过 xpath 查找元素的函数。
我从 Instagram 复制了 xpath 但它不起作用。
from selenium import webdriver
from getpass import getpass
from selenium.webdriver.common.action_chains import ActionChains
chromedriver = "C:\Users\Utente\Desktop\chromedriver"
driver = webdriver.Chrome(chromedriver)
usr = input ("Enter Your Email: ")
psw = getpass("Enter Your Password: ")
prfl = input('Enter the exactly name of the profile you want the picture: ')
driver.get("https://www.instagram.com")
login_elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_elem.click()
inputs = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]')
ActionChains(driver)\
.move_to_element(inputs[0])\
.click()\
.send_keys(usr)\
.move_to_element(inputs[1])\
.send_keys(psw)\
.perform()
login_button = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/button')
ActionChains(driver)\
.move_to_element(login_button)\
.click()\
.perform()
codio = driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]')
actions = ActionChains(driver)\
.move_to_element(codio)\
.click()\
.send_keys(prfl)\
.perform()
Here the xpath i copied from instagram
Here the error message
我稍微重写了您的代码,现在可以使用了:
from selenium import webdriver
from getpass import getpass
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chromedriver = "C:\Users\Utente\Desktop\chromedriver"
driver = webdriver.Chrome(chromedriver)
usr = input ("Enter Your Email: ")
psw = getpass("Enter Your Password: ")
prfl = input('Enter the exactly name of the profile you want the picture: ')
driver.get('https://www.instagram.com/accounts/login/')
wait = WebDriverWait(driver, 5)
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="username"]'))).send_keys(usr)
driver.find_element_by_xpath('//input[@type="password"]').send_keys(psw)
driver.find_element_by_xpath('//button[contains(text(), "Log in")]').click()
wait.until(EC.presence_of_element_located((By.XPATH, '//a[text()="Profile"]')))
我正在使用显式等待来使代码更稳定。
希望这能帮助您了解 selenium 的工作原理。
我只是想制作一个 python API 自动将我记录在我的 Instagram 页面上,但该程序似乎无法识别必须放置我的电子邮件地址的框和我的密码。我使用了通过 xpath 查找元素的函数。 我从 Instagram 复制了 xpath 但它不起作用。
from selenium import webdriver
from getpass import getpass
from selenium.webdriver.common.action_chains import ActionChains
chromedriver = "C:\Users\Utente\Desktop\chromedriver"
driver = webdriver.Chrome(chromedriver)
usr = input ("Enter Your Email: ")
psw = getpass("Enter Your Password: ")
prfl = input('Enter the exactly name of the profile you want the picture: ')
driver.get("https://www.instagram.com")
login_elem = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[2]/p/a')
login_elem.click()
inputs = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]')
ActionChains(driver)\
.move_to_element(inputs[0])\
.click()\
.send_keys(usr)\
.move_to_element(inputs[1])\
.send_keys(psw)\
.perform()
login_button = driver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/button')
ActionChains(driver)\
.move_to_element(login_button)\
.click()\
.perform()
codio = driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]')
actions = ActionChains(driver)\
.move_to_element(codio)\
.click()\
.send_keys(prfl)\
.perform()
Here the xpath i copied from instagram
Here the error message
我稍微重写了您的代码,现在可以使用了:
from selenium import webdriver
from getpass import getpass
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
chromedriver = "C:\Users\Utente\Desktop\chromedriver"
driver = webdriver.Chrome(chromedriver)
usr = input ("Enter Your Email: ")
psw = getpass("Enter Your Password: ")
prfl = input('Enter the exactly name of the profile you want the picture: ')
driver.get('https://www.instagram.com/accounts/login/')
wait = WebDriverWait(driver, 5)
wait.until(EC.presence_of_element_located((By.XPATH, '//input[@name="username"]'))).send_keys(usr)
driver.find_element_by_xpath('//input[@type="password"]').send_keys(psw)
driver.find_element_by_xpath('//button[contains(text(), "Log in")]').click()
wait.until(EC.presence_of_element_located((By.XPATH, '//a[text()="Profile"]')))
我正在使用显式等待来使代码更稳定。
希望这能帮助您了解 selenium 的工作原理。