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
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
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.
As an alternative, you can also use urlretrieve
method from Python 3.x as follows:
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")