Python 使用Selenium而非API向Instagram发布(上传)图像

Python 使用Selenium而非API向Instagram发布(上传)图像,python,json,selenium,upload,Python,Json,Selenium,Upload,背景: 我想解决这个问题已经有一段时间了。我正在使用Selenium chrome webdriver从桌面访问Instagram的移动版。我的目标是创建一个脚本来自动将图像发布到帐户。使用Python,我已经自动化了所有的事情,直到我需要选择文件的部分 问题: 一旦我点击CreatenewPost按钮,gui文件选择器就会打开,我不知道如何与它交互来选择文件 尝试: post_btn.send_keysr'/Path/To/image.jpg' post_btn.send_keys'/Path

背景:

我想解决这个问题已经有一段时间了。我正在使用Selenium chrome webdriver从桌面访问Instagram的移动版。我的目标是创建一个脚本来自动将图像发布到帐户。使用Python,我已经自动化了所有的事情,直到我需要选择文件的部分

问题:

一旦我点击CreatenewPost按钮,gui文件选择器就会打开,我不知道如何与它交互来选择文件

尝试:

post_btn.send_keysr'/Path/To/image.jpg'

post_btn.send_keys'/Path/To/image.jpg'

我认为,这种方法的问题在于没有输入字段来发送字符串

我读了一些其他关于使用autoit的帖子,但是我在Mac上

以下是post按钮所在的范围:

<span class="glyphsSpriteNew_post__outline__24__grey_9 u-__7" aria-label="New Post"></span>
问题:

是否有一种使用python通过文件选择器gui“打开”上载文件的方法? 更好的是,我能完全绕过打开文件选择器吗

谢谢

编辑:


我想阻止我解决这个问题的原因是我不理解数据是如何发送和接收的。既然它不是html输入,这是否意味着它是JSON?如何确定数据图像是如何发送到下一页的?

我在结束时尝试了这个方法,我清楚地看到没有办法绕过文件选择器。也就是说,您需要使用外部工具来自动化文件选择器。以我的经验来看,这可能最适合这份工作。

IDK如果这对你有用的话,因为它已经很长时间了。但是对于任何需要它的人,这里有完整的教程

进口:

import os
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import autoit
import time
from selenium.webdriver.common.keys import Keys
所以首先你需要一个模拟器。 我发现的最好的方法是使用chromedriver.exe,如果您使用chrome作为浏览器,您已经需要它了。因此,要制作模拟器,您需要为驱动程序编写以下代码:

mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(chrome_options = chrome_options)
然后您登录:

driver.get('https://www.instagram.com/accounts/login/')

time.sleep(2)
driver.find_element_by_name("username").send_keys("Your Username")
driver.find_element_by_name("password").send_keys("Your Password") 
driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button""").click()

time.sleep(2)

driver.get('https://www.instagram.com/' + username)
命名您的文件它需要是全名:

ImagePath = 'Your File Location'
最后一步是:

ActionChains(driver).move_to_element( driver.find_element_by_xpath("""//*[@id="react-root"]/section/nav[2]/div/div/div[2]/div/div/div[3]""")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 3)
autoit.control_set_text(handle, "Edit1", dir_path)
autoit.control_click(handle, "Button1")

time.sleep(2)

driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()

time.sleep(2)

txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('')
txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('test') # Descrition
txt.send_keys(Keys.ENTER)

driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()

所有这一切只是转到上传页面,使用autoit在窗口中导航,选择一个文件并进行选择。然后添加一个描述并分享帖子。

我在某处找到了这个,它在python中运行良好。 导入pyautogui pyautogui.write'D:\opt\FitnessStudio\picofme.jpg'
Pyatogui.press'enter'

是的,我在发布这篇文章后,昨晚用SikuliX构建了这个脚本,但它感觉超级黑客,实际上比黑客还要糟糕。我觉得这让我有点恶心:^P.我觉得必须有更好的方法来实现这一点。@ShaneCheek sikuli还在吗?无法访问该网站:您是如何访问Instagram的移动版的?我尝试了chrome\u选项。add\u argument-window-size=411411但它会打开桌面版本
# ------------ Imports --------------- 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import random

# ------------ Def Random --------------
def sleepTime(start):
    _sleep = (random.randint(start, 20))
    time.sleep(_sleep)

#  ---------------- WebDriver -----------------
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://www.instagram.com/accounts/login/')
driver.email = '*instaID*'
driver.password = '*instapswd*'
driver.username = '*instapageusernameyouwanttofollow*'
sleepTime(5)
try:
    emailInput=driver.find_element_by_xpath("//input[@name = 'username']")
    passwordInput=driver.find_element_by_xpath("//input[@name = 'password']")
except (NoSuchElementException, ElementNotVisibleException) as exceptions:
    pass
emailInput.send_keys(driver.email)
passwordInput.send_keys(driver.password)
passwordInput.send_keys(Keys.ENTER)
sleepTime(7)

# ---pop up handle---
NotNow=driver.find_element_by_xpath("//button[text() = 'Not Now']").click()
sleepTime(5)
# ---------- Going to folower list ----------
driver.get('https://www.instagram.com/' + driver.username + '/')
sleepTime(7)
driver.get('https://www.instagram.com/' + driver.username + '/followers/')
Followers=driver.find_element_by_xpath("//a[@class='-nal3 ']").click()
sleepTime(7)
# -------------------------------------------
#find all li elements in list
fBody  = driver.find_element_by_xpath("//div[@class='isgrP']")
scroll = 0
for j in (1,(random.randint(4, 20))):
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
    sleepTime(10)
# for k in (1,4):
#     driver.execute_script('arguments[0].scrollDown = arguments[0].scrollDown + arguments[0].offsetHeight;', fBody)
#     time.sleep(10)

count = 0
while scroll < 500: # scroll 500 times
    sleepTime(10)
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
    for i in (0,(random.randint(5, 20))):
        try:
            Follow=driver.find_element_by_xpath("//button[text() = 'Follow']")
            Follow.click()
            print(count)
            count = count + 1
            driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)                
            time.sleep(10)
        except NoSuchElementException:
            pass
    sleepTime(8)
    # driver.executeScript("window.scrollBy(0,100)")
    scroll += 1
    driver.execute_script('arguments[0].scrollTop = arguments[0].scrollTop + arguments[0].offsetHeight;', fBody)
    sleepTime(4)
driver.close()