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
  Using OpenCV and image path is invalid AudunNilsen 5 324 Yesterday, 05:28 PM
Last Post: snippsat
  Count image's colors very fast flash77 18 1,226 Mar-05-2024, 06:12 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,666 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  error "cannot identify image file" part way through running hatflyer 0 578 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  Failed attempts to load Microsoft Appstore Python DLLs piyushd 0 382 Oct-31-2023, 10:43 AM
Last Post: piyushd
  Mirror Video Image in realtime makingwithheld 1 381 Oct-30-2023, 02:45 PM
Last Post: Larz60+
  howto get size of a ctk image? janeik 2 741 Oct-03-2023, 03:49 AM
Last Post: janeik
  Use of PIL.Image nafshar 12 1,871 Sep-07-2023, 11:02 PM
Last Post: nafshar
Question image manipulation (cropping and MetaData) SpongeB0B 4 1,079 Jul-03-2023, 06:35 PM
Last Post: SpongeB0B
  Save image from outlook email cubangt 1 632 Jun-07-2023, 06:52 PM
Last Post: cubangt

Forum Jump:

User Panel Messages

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