Python Forum
Upload image to Instagram using python/selenium using image URL failed, need help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Upload image to Instagram using python/selenium using image URL failed, need help
#1
Hello;
I try to post images to Instagram using python selenium, but when I want to use the image URL I get an error.
when I use an absolute path to the image file from my computer it works and the image is submitted,
Appreciate your help on this.

this is snipet of code I used:

from selenium import webdriver
post_image = ser.find_element(By.XPATH, "//div[contains(@class,'_C8iK')]//input[contains(@type,'file')]")
post_image.send_keys("C:\\Users\\myName\\Desktop\\images\\image-1.jpg")
but when I try to image URL which is stored in dropbox share folder, it does not work,
example:
post_image = ser.find_element(By.XPATH, "//div[contains(@class,'_C8iK')]//input[contains(@type,'file')]")
post_image.send_keys("https://www.dropbox.com/s/fe16a48r2l2wzvh/apple.jpg")
I have tried it on my computer, running windows 7 64 bit, python 3.8, selenium 4.1.0
when I run with image URL I get the error: path is not absolute

I also tested it on pythonanywhere, the first method worked over there as well, when I put the image on pythonanywhere, but when using image URL from dropbox I get this error:
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : https://www.dropbox.com/s/fe16a48r2l2wzvh/apple.jpg
(Session info: headless chrome=90.0.4430.212)
Reply
#2
.send_keys() dos not support url download.
So to make it work most first download to local disk and then upload string path to .send_keys().
So example can do it like this.
import requests
from pathlib import Path

def download_url(url):
    response = requests.get(url)
    file_name = Path(url).name
    with open(file_name, 'wb') as f:
        f.write(response.content)

if __name__ == '__main__':
    url = 'https://ucf3277436240317b06cccc5f84d.previews.dropboxusercontent.com/p/thumb/ABfdW-uYkR8j11h90eejPB1jn_6HetPyyIVQw0n3Yk7L6g_d4rwBD3O7XhWMdzD6D9BXfg82o94EDmnHZeNkx8O6x_1fgsZHPwRssC1B8AroxaTi5G35y1IilExG1jmjfCjfGZF98JzAvnRRrUT2G11bQTsOqEyVvlR9GAnZ84Oq18lnHp9Enym1Rlz3R4tIqOn46loci4HqL402gH1lsXCjDCgCu6wNMbAukbHSvnyfhZztaOkRFSgQaiDUn1ZKC-c8nd_4vOv51qUhfZusI5lR8JOx1sSZibAgsKICYsNCbrJeCQbcZIMcENksvvne9ohmdkWUtL7wU9n0n4HDfmkn-mJdU0MjkLSH1D4ejumv92OVPpeeWbN5S3QglVJx-7I/p.jpeg'
    download_url(url)
I use your link here https://www.dropbox.com/s/fe16a48r2l2wzvh/apple.jpg,but remember that this is not the direct download link.
It's the link to Dropbox UI interface,so need to get long autogenerated real image url as i do(open image in new tab to get it).
Reply
#3
thank you for your response and the code,
I tried your code on my computer,

1- but I still get the same error:
Quote:selenium.common.exceptions.WebDriverException: Message: unknown error: path is not absolute: https://ucf3277436240317b06cccc5f84d.pre...-7I/p.jpeg

2- the image has been downloaded to my computer.
3- I could not generate the link as you described, opening the link in a new tab, will have the same name.
I used your generated link anyway,
I even tried using the direct link for dropbox using the method here, but I got the same error.

I have used
import requests
from pathlib import Path

and this is the snippet I tried:

    response = requests.get(url)
    file_name = Path(url).name
    with open(file_name, 'wb') as f:
        f.write(response.content)
 
if __name__ == '__main__':
    url = 'https://ucf3277436240317b06cccc5f84d.previews.dropboxusercontent.com/p/thumb/ABfdW-uYkR8j11h90eejPB1jn_6HetPyyIVQw0n3Yk7L6g_d4rwBD3O7XhWMdzD6D9BXfg82o94EDmnHZeNkx8O6x_1fgsZHPwRssC1B8AroxaTi5G35y1IilExG1jmjfCjfGZF98JzAvnRRrUT2G11bQTsOqEyVvlR9GAnZ84Oq18lnHp9Enym1Rlz3R4tIqOn46loci4HqL402gH1lsXCjDCgCu6wNMbAukbHSvnyfhZztaOkRFSgQaiDUn1ZKC-c8nd_4vOv51qUhfZusI5lR8JOx1sSZibAgsKICYsNCbrJeCQbcZIMcENksvvne9ohmdkWUtL7wU9n0n4HDfmkn-mJdU0MjkLSH1D4ejumv92OVPpeeWbN5S3QglVJx-7I/p.jpeg'
    download_url(url)

post_image = ser.find_element(By.XPATH, "//div[contains(@class,'_C8iK')]//input[contains(@type,'file')]")
post_image.send_keys(url)
Thanks.
Reply
#4
Most use local file path,now you just use url as before.
The code make image file on your local drive p.jpeg.
No path is given so p.jpeg will be in same folder as you run .py file in.
Let say you download to same folder as you used before,then this will be the path to use:
post_image.send_keys("C:\\Users\\myName\\Desktop\\images\\p.jpeg")
Reply
#5
Thanks for your reply,
so every time I want to post an image I have to run the script to get the image first,
then I can upload it, is that right?
so, in that case, isn't easier to download all of the images from dropbox to my local machine, and run code as before.
if I understand it correctly.
Reply
#6
(Feb-22-2022, 07:24 PM)greenpine Wrote: so every time I want to post an image I have to run the script to get the image first,
then I can upload it, is that right?
Yes.
(Feb-22-2022, 07:24 PM)greenpine Wrote: so, in that case, isn't easier to download all of the images from dropbox to my local machine, and run code as before.
Yes it's easier to get all images to your local machine,with code as show or manually do it yourself.
So if want upload many images make a code that feed .send_keys() with image files from local drive.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Placing image button on top of background image Sintek 1 366 Mar-02-2025, 05:36 PM
Last Post: Sintek
  Problem When using canny edge detection,black image returned rickyw2777 1 345 Feb-17-2025, 03:22 AM
Last Post: rickyw2777
  Why it gives me a black image and libpng warning: iCCP rickyw2777 1 342 Feb-16-2025, 08:26 PM
Last Post: rickyw2777
  QR code creation with image Raja31 1 519 Jan-15-2025, 11:17 AM
Last Post: Larz60+
Photo image generation with text style Belialhun 0 605 Oct-08-2024, 01:53 PM
Last Post: Belialhun
  Sudden Extremely Slow / Failed Python Imports bmccollum 1 942 Aug-20-2024, 02:09 PM
Last Post: DeaD_EyE
  How can I upload a .gz file to the Swift Object Storage using Python swift client? Hanginium65 0 674 Jul-24-2024, 03:24 PM
Last Post: Hanginium65
  Product Image Download Help Required pythonustasi 5 1,184 Jul-21-2024, 08:12 PM
Last Post: snippsat
  tkinter photo image problem jacksfrustration 5 2,862 Jun-27-2024, 12:06 AM
Last Post: AdamHensley
Photo image error pyc0de 2 1,575 Mar-23-2024, 06:20 PM
Last Post: pyc0de

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020