简体   繁体   English

selenium.common.exceptions.InvalidArgumentException:消息:尝试通过selenium通过url上传图像时找不到文件

[英]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")

send_keys(*value) send_keys(*值)

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

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. 如果你的用例是使用Selenium进行文件上传,你必须有下载在本地系统中的文件,并将该文件的绝对路径中的参数send_keys()方法。


Alternative (urllib.request.urlretrieve) 替代(urllib.request.urlretrieve)

As an alternative, you can also use urlretrieve method from Python 3.x as follows: 或者,您也可以使用Python 3.x中的 urlretrieve方法,如下所示:

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

  • 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.

相关问题 selenium.common.exceptions.InvalidArgumentException:消息:无效参数:使用 Selenium 上传文件时找不到文件错误 Python - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Python selenium.common.exceptions.InvalidArgumentException:消息:无效参数:使用 Selenium 调用 send_keys() 时未找到文件错误 - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error invoking send_keys() using Selenium URL 必须是字符串 selenium - selenium.common.exceptions.InvalidArgumentException:消息:无效参数:'url' 必须是字符串 - URL must be a string selenium - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string selenium.common.exceptions.InvalidArgumentException:消息:无效参数:使用 selenium webdriver 添加 cookie 时缺少“cookie” - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie' while adding cookies using selenium webdriver selenium.common.exceptions.InvalidArgumentException:消息:使用 Selenium Webdriver 通过 Python 调用 get() 时参数无效错误 - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python selenium.common.exceptions.InvalidArgumentException:消息:无效参数错误调用 get() 与使用 Selenium Python 从文本文件读取的 url - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() with urls read from text file with Selenium Python selenium.common.exceptions.InvalidArgumentException:消息:无效参数:在 Selenium 中切换帧时缺少“元素”错误 - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' error when switching frame in Selenium Python selenium 用于撰写评论谷歌地图(selenium.common.exceptions.InvalidArgumentException:消息:无效参数:无效定位器) - Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator ) selenium.common.exceptions.InvalidArgumentException:消息:无效参数:“使用”必须是使用等待和预期条件的字符串 - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'using' must be a string using waits and expected conditions CssSelector 是我可以用于 shadowroot 的唯一方法吗? selenium.common.exceptions.InvalidArgumentException:消息:无效参数:无效定位器 - Is CssSelector the only way I can use for shadowroot? selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM