[英]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
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 要在Python 3.X中检索文件,您可以尝试
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")
As per the documentation of send_keys()
simulates typing into the element. 根据send_keys()
的文档,模拟了元素的键入。
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: 但是根据您的代码试用,您将URL作为字符串传递,因此您看到的错误为:
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. 如果你的用例是使用Selenium进行文件上传,你必须有下载在本地系统中的文件,并将该文件的绝对路径中的参数send_keys()
方法。
As an alternative, you can also use urlretrieve
method from Python 3.x as follows: 或者,您也可以使用Python 3.x中的 urlretrieve
方法,如下所示:
Copy a network object denoted by a URL to a local file. 将URL表示的网络对象复制到本地文件。 If the URL points to a local file, the object will not be copied unless filename is supplied. 如果URL指向本地文件,则除非提供文件名,否则不会复制该对象。 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). 返回一个元组(文件名,标头),其中filename是可以在其中找到对象的本地文件名,标头是由urlopen()
返回的对象的info()方法返回的(对于远程对象)。 Exceptions are the same as for urlopen()
. 异常与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")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.