Posting (uploading) An Image To Instagram Using Selenium Not Using An Api
Solution 1:
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_chainsimportActionChainsfrom selenium.webdriver.chrome.optionsimportOptionsimport autoit
import time
from selenium.webdriver.common.keysimportKeys
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.
Solution 2:
I tried this at my end and I can clearly see that there is no way to bypass file selector. That being said, you need to use external tool to automate file selector. In my experience Sikuli could be best fit for this job.
Solution 3:
I found this somewhere and it works fine in python. import pyautogui pyautogui.write('D:\opt\FitnessStudio\picofme.jpg') pyautogui.press('enter')
Solution 4:
# ------------ 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()
Solution 5:
I have accomplished posting images to Instagram using execute_script
and setting mobileEmulation
. . .
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation", {"deviceName": "Nexus 5"})
driver = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options)
. . .
. . .
driver.execute_script(
"HTMLInputElement.prototype.click = function() { " +
" if(this.type !== 'file') HTMLElement.prototype.click.call(this); " +
"}; ")
driver.find_element_by_xpath('//div[@data-testid="new-post-button"]').click()
driver.find_elements_by_tag_name('input')[-1].send_keys(<img_path>)
driver.execute_script("delete HTMLInputElement.prototype.click")
. . .
If you have any problems implementing this, check out my code at repo instagram-post-automation
Post a Comment for "Posting (uploading) An Image To Instagram Using Selenium Not Using An Api"