python - 使用 Python 在 selenium Webdriver 中通过 Url 上传图像
问题描述
driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("https://ercess.com//images//events//-Blockchain-2019-36613-banner.png")
有什么办法可以使它起作用吗?
[错误] selenium.common.exceptions.InvalidArgumentException:消息:找不到文件:https ://ercess.com//images//events//-Blockchain-2019-36613-banner.png
解决方案
您首先需要将图像下载到您的计算机,然后将其上传...
您可以使用requests
:
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)
然后发送/path/to/image.jpg
:
driver.find_element(By.XPATH, "//*[@id='upl-zone']/input").send_keys("/path/to/image.jpg")
或者您可以使用您将使用Legacy interface
的:urllib
urlretrieve
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")
编辑:
要使用 send_keys 发送文件的路径,您可以使用pathlib
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)
希望这对你有帮助!
推荐阅读
- reactjs - iOS 和 Android 是否支持带有 react native 的动画 GIF 图像?
- scikit-learn - sklearn.linear_model.Lasso中参数“选择”的作用是什么
- ios - 如何将 tableview 单元格附件应用于 tableview 记录
- https - 使用 GOST 2012 (ГОСТ Р 34.10-2012) 和 Python 2.7.15+ 的 https 连接:
- c# - 指定的演员在 Acumatica ERP 系统的新主屏幕中无效
- c++ - C++ 添加检查以避免读取两个参数
- c# - 如何将 Automapper 与 Autofac 一起使用
- oracle - 如何使用 get_ddl 获取分区脚本?
- r - R 等效于 SAS EG 工艺流程
- c# - 在 Cassandra 中处理 C# 嵌套类