1

I need to upload image via external url, i found examples only that shows how to upload locally stored images. This what i tried and this didn't work.

driver.find_element_by_id("attachFile_nPaintUploadAll").send_keys("http://bdfjade.com/data/out/89/5941587-natural-image-download.jpg")

Error message:

selenium.common.exceptions.InvalidArgumentException: Message: File not found: http://bdfjade.com/data/out/89/5941587-natural-image-download.jpg
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Andrew
  • 1,507
  • 1
  • 22
  • 42
  • Possible duplicate of [How to pass image URL from from a HTTP server in selenium](https://stackoverflow.com/questions/27839517/how-to-pass-image-url-from-from-a-http-server-in-selenium) – Shivam Mishra Jul 20 '18 at 05:26

2 Answers2

2

send_keys(*value)

As per the documentation of send_keys() simulates typing into the element.

  • send_keys(*value)
  • Args :

    value - A string for typing. For setting file inputs, this could be a local file path.
    
  • Use this to set file inputs:

    driver.find_element_by_id("attachFile_nPaintUploadAll").send_keys("path/to/profilepic.gif")
    

But as per your code trial as you are passing an url as a string hence you see the error as:

selenium.common.exceptions.InvalidArgumentException: Message: File not found: http://bdfjade.com/data/out/89/5941587-natural-image-download.jpg

Solution

If your usecase is to use Selenium for file uploading you have to have the download the file in your local system and pass the absolute path of the file as an argument within send_keys() method.


Alternative (urllib.request.urlretrieve)

As an alternative, you can also use urlretrieve method from Python 3.x as follows:

urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)

  • Copy a network object denoted by a URL to a local file. If the URL points to a local file, the object will not be copied unless filename is supplied. Return a tuple (filename, headers) where filename is the local file name under which the object can be found, and headers is whatever the info() method of the object returned by urlopen() returned (for a remote object). Exceptions are the same as for urlopen().

  • Code Block:

    import urllib.request
    
    urllib.urlretrieve("http://andrew.com/selfie.jpg", "andrew_selfie.jpg")
    driver.find_element_by_id("attachFile_nPaintUploadAll").send_keys("andrew_selfie.jpg")
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • maybe in Code Block: `urllib.request.urlretrieve("http://andrew.com/selfie.jpg", "andrew_selfie.jpg")` – Dokuch Jun 30 '23 at 12:13
1

Try to get the file first and then upload it:

import urllib

urllib.urlretrieve("http://bdfjade.com/data/out/89/5941587-natural-image-download.jpg", "5941587-natural-image-download.jpg")
driver.find_element_by_id("attachFile_nPaintUploadAll").send_keys("5941587-natural-image-download.jpg")

To retrieve file in Python 3.X you can try

urllib.request.urlretrieve("http://bdfjade.com/data/out/89/5941587-natural-image-download.jpg", "5941587-natural-image-download.jpg")

or

import requests

with open("5941587-natural-image-download.jpg", "wb") as f:
    f.write(requests.get("http://bdfjade.com/data/out/89/5941587-natural-image-download.jpg").content)

You can remove the file then with

import os

os.remove("5941587-natural-image-download.jpg")
Andersson
  • 51,635
  • 17
  • 77
  • 129