pythonseleniumselenium-webdriverwebdriverurlretrieve

selenium.common.exceptions.InvalidArgumentException: Message: File not found while trying to upload image by url through selenium


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

Solution

  • send_keys(*value)

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

    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)