Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
346 views
in Technique[技术] by (71.8m points)

python - Posting (Uploading) an image to Instagram using Selenium not using an API

Background:

I've been trying to solve this for a while now. I'm using Selenium (chrome webdriver) to access the mobile version of Instagram from the desktop. My goal is to create a script to automate posting an image to an account. Using Python I have automated everything up to the part where I need to select the file.

Issue:

Once I click the create new post button the gui file selector opens and I cannot figure out how to interact with it to select the file.

Tried:

post_btn.send_keys(r'/Path/To/image.jpg')

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

The trouble, I think, with this method is there is not input field to send the string to.

I read some other posts about using autoit and that working, but I'm on a Mac.

Here's the span the post button lives in:

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

Question:

Is there a way to 'open' (upload) a file through a file selector gui using python? Better, can I bypass opening the file selector at all?

Thanks!

Edit:

I think what's stopping me from solving this is that I don't understand how the data is being sent and received. Since it's not an html input does that mean it is JSON? How do I figure out how the data (image) is being sent to the next page (https://www.instagram.com/create/style/)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

IDK if this will be useful to you because it has been so long.But to anyone who needs it here is the full tutorial,

Imports:

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

So First you need a emulator. The best method that I found was using chromedriver.exe which you already need if you are using chrome as the browser. So to make the emulator you need this bit of code for the driver:

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)

Then you login:

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)

Name your file (It needs to be full name):

ImagePath = 'Your File Location'

final step is this:

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()

All this does is go the the upload page and use autoit to navigate through windows to select a file and choose. Then just adds a description and shares the post.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...