使用Python通过SeleniumWebDriver中的Url上传图像

使用Python通过SeleniumWebDriver中的Url上传图像,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,有什么办法可以让它起作用吗 [错误]selenium.common.exceptions.InvalidArgumentException:消息:未找到文件: 您首先需要将图像下载到您的计算机,然后将其上载 您可以使用请求: driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("https://ercess.com//images//events//-Blockchain-2019-36613-banner.p

有什么办法可以让它起作用吗

[错误]selenium.common.exceptions.InvalidArgumentException:消息:未找到文件:


您首先需要将图像下载到您的计算机,然后将其上载

您可以使用
请求

driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("https://ercess.com//images//events//-Blockchain-2019-36613-banner.png")
然后发送
/path/to/image.jpg

import requests

URL = "https://ercess.com//images//events//-Blockchain-2019-36613-banner.png"
picture_req = requests.get(URL)
if picture_req.status_code == 200:
    with open("/path/to/image.jpg", 'wb') as f:
        f.write(picture_req.content)

或者您可以使用
urllib
的,您将使用
urlretrieve

driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("/path/to/image.jpg")
编辑:

要使用send_键发送文件路径,可以使用


希望这对你有帮助

当我在windows资源管理器中键入url手动上载时,我已经删除了我的答案。是否有任何具体原因这样做<代码>驱动程序.get(url)也可以正常工作。它向我显示错误:消息:未找到文件:File\u name.png您在使用什么
urllib
请求
?我正在使用urlib.urlretrieve@moshet文件下载到我运行的目录中,但当我尝试上载时,它告诉我上面的错误@Moshe@Nitish我不是靠电脑。。。但请看这个:
import urllib.request

URL = "https://ercess.com//images//events//-Blockchain-2019-36613-banner.png"
urllib.urlretrieve(URL, "file_name.png")
driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("file_name.png")
from pathlib import Path

# `cwd`: current directory is straightforward
cwd = Path.cwd()
# using "F"string for format you can use: image_file_name = str(cwd) + "\" + "file_name.png" 
image_file_name = fr"{cwd}\file_name.png"
# this print is just to show the image_file_name   
print(image_file_name)