繁体   English   中英

"如何使用 python 使用 selenium 在 instagram 中上传图像?"

[英]How to upload an image in instagram using selenium using python?

我想使用 selenium 在 Instagram 上上传照片。 我写了这段代码,但给了我一个错误。

代码:

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("username")

my_password=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("*******")
time.sleep(10)

login=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
login.click()

time.sleep(5)
upload=driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[3]/div/button/div/svg')
upload.click()
img='"C:/Users/lucky/Downloads/old-black-background-grunge-texture-dark-wallpaper-blackboard-chalkboard-room-wall.jpg"'
time.sleep(2)
img_upload=driver.find_element_by_xpath('/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/div/div/div[2]/div/button')

img_upload.send_keys(img)

time.sleep(30)

试试这个

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import pyautogui

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("------")

my_password=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("-------")
time.sleep(10)

login=driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
login.click()

time.sleep(5)
upload=driver.find_element_by_xpath('//div[@class="QBdPU "]')
upload.click()
time.sleep(2)
img_upload=driver.find_element_by_xpath('/html/body/div[8]/div[2]/div/div/div/div[2]/div[1]/div/div/div[2]').click()
time.sleep(2)
path = "D:\Pictures\ph.png" # your imagepath
pyautogui.write(path) 
time.sleep(2)
pyautogui.press('enter')


完整的上传过程需要执行更多步骤。

建议而不是 time.sleep 使用等待

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM